G
G
Gloynus Aleos2019-03-10 20:25:54
PHP
Gloynus Aleos, 2019-03-10 20:25:54

PhpSpreadsheet how to save changes to the same file?

How to load data from an XLSX file - I know, I practice.
I know how to create an XLSX file with the necessary data, but I don’t practice.
I don’t know and can’t understand how to load data from an XLSX file, and then save the changes into it (right into a specific cell, without touching others).
I beg you to explain this to me.
I load and work with data like this.

use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;

$file1 = '123.xlsx';
$Reader = new Xlsx();

$deck_table = $Reader->load($file1);
$deck_table->setActiveSheetIndexByName('Страница 2');

$deck_base = $deck_table->getActiveSheet()->rangeToArray('A1:AO100', '0', true, true, true);

foreach ($deck_base as $key => $value)
{
 // в общем, тут длинный цикл, который по очереди меняет данные в нужных ячейках вот таким образом
 // $deck_table->getActiveSheet()->setCellValue($key . $result, $aaa);
 // и если обратиться к тем ячейкам, то там действительно есть данные
 //  echo $deck_table->getActiveSheet()->getCell($key . $result)->getValue() . '<br>';
}
// но что мне написать, чтобы потом все эти изменения сохранить в тот же файл, из которого я взял данные?

Attempts to use the methods in this tutorial failed: https://phpspreadsheet.readthedocs.io/en/stable/to... . As if I can only read or only create.
The code from their main page also does not allow you to save changes.
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');

$spreadsheet = $Reader;
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');

The file is easy to create. Created.
But here's how I get here:
$writer = new Xlsx($spreadsheet); $writer->save('hello world.xlsx');
- transfer my data?
Or what should be done?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
Gloynus Aleos, 2019-03-10
@Gloynus_Aleos

It turned out that the error was in some formula (more precisely, in the type of some formulas from interactive tables).
This line excludes the processing of formulas: and the file is saved. In sum, saving changes to the same file looks like this:

$writer = IOFactory::createWriter($deck_table, "Xlsx");
$writer->setPreCalculateFormulas(false);
$writer->save($file1);

But it affects everything. All styles, all formulas, all that is.
Is there a way to change data only in specific cells without touching anything else?

G
Galdar Turin, 2019-03-11
@Galdar

You can apply a loop and various functions to setCellValue() , output from the database from an array, etc.

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1'), 'Text')->setCellValue('B1'), 'Text').....;

Z
ZXSpectrum48k, 2021-11-04
@ZXSpectrum48k

There is a solution here https://stackoverflow.com/questions/58213250/how-t...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question