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

时间:2015-10-26 04:40:41

标签: php excel phpexcel

我正在使用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版本的用户会遇到此问题。对于他们来说,这些细胞看起来是空白的。

1 个答案:

答案 0 :(得分:3)

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

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