Hi there is a way to edit uploaded Excel (xlsx) document in PHP. Document is very simple (table with item names, price, quantity). I suppose PHPExcel able only read document but not edit? Any advices?
Asked
Active
Viewed 1.9k times
5
-
4Why do you think PHPExcel will be unable to edit it? They have a `PHPExcel_Writer_Excel2007` class that seems to handle xlsx correctly. – DCoder May 27 '12 at 18:11
-
2Simply reading the PHPExcel site front page should have told you that it's read and write: phrases like "allow you to write to and read from" are the clue we give. – Mark Baker May 27 '12 at 18:21
3 Answers
6
You can use PHPExcel to edit a document as well, check out these threads for more information:
-
1Oh thanks! Its can be confusing when using different documents for upload. I'll try to explain crealry. There is a system which generates excel documents (there is three templates with different layouts but generates same table in different style) so in provided examples the edited cells was chosen manualy ->setCellValue('A1', 'Hello') , So how to find out which field has the information, and after editing how to find out which cell was edited and save the changes? – dojo May 27 '12 at 18:23
-
1@DeividasJuškevičius: You can look at official docs of PHPExcel or their support forum for more info actually. – Sarfraz May 27 '12 at 18:28
-
PHPExcel is archived and no longer supported. Its predecessor PHPSpreadsheet is available however – Bill Garrison Mar 15 '19 at 17:16
0
Please try this approach. For Example, I want to edit/update the column(expiryDate) in the sheet (sheet1).
$localFilePath = "C:/xampp/htdocs/Projects/abc.xlsx";
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$reader->setLoadSheetsOnly(["sheet1"]);
$spreadsheet = $reader->load($localFilePath);
$worksheet = $spreadsheet->setActiveSheetIndex(0);
$column = NULL;
foreach($worksheet->getRowIterator() as $index => $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells,
foreach($cellIterator as $subIndex => $cell) {
$value = $cell->getValue();
if($index == 1 && $value == "expiryDate"){
$column = $subIndex;
}
if((empty($value) || $value == "") && $column == $subIndex && $index != 1){
$cell->setValue("2050-12-31 00:00:00");
}
}
}
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");
$writer->save($localFilePath);

Shirjeel Ahmed Khan
- 257
- 1
- 5
- 12