我正在使用php excel codeplex,我正在填充这样的单元格:
$objWorksheet->setCellValue('B12', "='Other sheet'!D38");
我成功保存文件,当我打开它时,公式就在那里,但它不会渲染计算值。如果我将公式复制并粘贴到另一个单元格,它运行正常,所以,这不是公式语法的问题。如何在保存之前强制执行公式?我试过了:
\PHPExcel_Calculation::getInstance($objPHPExcel)->disableCalculationCache();
\PHPExcel_Calculation::getInstance($objPHPExcel)->clearCalculationCache();
没有成功...
这是解决问题的示例代码:
$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);
我添加了这段代码,它对我有用。
PHPExcel_Calculation::getInstance($excelObj)->disableCalculationCache();
PHPExcel_Calculation::getInstance($excelObj)->clearCalculationCache();
$writer = PHPExcel_IOFactory::createWriter($excelObj, 'Excel2007');
$writer->setPreCalculateFormulas(true);
$writer->save($output_file);
对于那些使用Laravel Excel的人,您可以在excel.php
配置文件中轻松设置导出期间的计算。只需将选项calculate
设置为true即可。
/*
|--------------------------------------------------------------------------
| Pre-calculate formulas during export
|--------------------------------------------------------------------------
*/
'calculate' => true,