Home > other >  PhpExcel write/save onto existing excel file
PhpExcel write/save onto existing excel file

Time:12-27

First of all, I have searched for my problem in both stackoverflow and on the forms, but I could not find exact question and its solution. (enter image description here

I load/read this file with PHPexcel (read/load works fine) and I fill up with price values on some empty cells, then I am trying to save the file back with below code.

require_once __DIR__ . '/src/Classes/PHPExcel.php';
require_once __DIR__ . '/src/Classes/PHPExcel/IOFactory.php';

// read existing excel file
$readphpexcel = PHPExcel_IOFactory::load("polimer_uzex.xlsx");

// read the worksheet named "fetched"
$fetched = $readphpexcel->getSheetByName("fetched");

// make an array from all data on the worksheet
$exceldatas = $fetched->toArray(null, true, true, true);


/// ... some cell value modifications take place here on $exceldatas


// write to excell
$fetched->fromArray($exceldatas, null, 'A1', true);
$objWriter = PHPExcel_IOFactory::createWriter($readphpexcel, 'Excel5');// Excel 2010
$objWriter->save("polimer_uzex.xlsx");

After saving, I am getting below error (“Excel cannot open the file because the file format or file extension is not valid ... ") enter image description here

CodePudding user response:

excel5 is for xls ( excel2007 is for xlsx)

Hence, change the write to excel block to

$fetched->fromArray($exceldatas, null, 'A1', true);
$objWriter = PHPExcel_IOFactory::createWriter($readphpexcel, 'Excel2007');// Excel 2010
$objWriter->save("polimer_uzex.xlsx");
  • Related