PHP Excel 为公式单元格提供空白输出,直到单击启用编辑

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

我正在使用 phpexcel 创建一个 xlsx 文件。我在文件中有很多公式。下载文件后,某些公式单元格(并非全部)显示为空白,直到我单击“启用编辑”选项。

我正在使用以下代码:

$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');

使用没有启用编辑选项的早期 Office 版本的用户会出现此问题。对于他们来说,这些单元格显示为空白。

我在其他地方看到了这个答案。如果其他人正在寻找答案。

$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);