我正在使用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,