Answer the question
In order to leave comments, you need to log in
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>';
}
// но что мне написать, чтобы потом все эти изменения сохранить в тот же файл, из которого я взял данные?
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');
$writer = new Xlsx($spreadsheet); $writer->save('hello world.xlsx');- transfer my data?
Answer the question
In order to leave comments, you need to log in
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);
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').....;
There is a solution here https://stackoverflow.com/questions/58213250/how-t...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question