Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Excel Gives blank output for a formula cell until enable Editing is clicked

I am creating an xlsx file using phpexcel. I have many formulas in the file. when the file is downloaded, some of the formula cells (not all) appear blank until I click the Enable Editing option.

I am using the following code :

$objPHPExcel = new PHPExcel();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$xlsName.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');

The problem here occurs with users who are using previous Office versions who don't have the enable editing option. For them these cells appear blank.

like image 436
Simone Avatar asked Oct 26 '15 04:10

Simone


1 Answers

I came across this answer somewhere else. If anyone else is looking for answers.

$spreadsheet = new \PHPExcel();
$writer = new \PHPExcel_Writer_Excel2007($spreadsheet);

//Do things with the $spreadsheet

//This is the solution, do it before saving
$writer->setPreCalculateFormulas(); 
$writer->save($saving_filepath);
like image 117
balslev Avatar answered Oct 21 '22 06:10

balslev