Php excel公式未在保存的电子表格中运行

问题描述 投票:4回答:3

我正在使用php excel codeplex,我正在填充这样的单元格:

$objWorksheet->setCellValue('B12', "='Other sheet'!D38");

我成功保存文件,当我打开它时,公式就在那里,但它不会渲染计算值。如果我将公式复制并粘贴到另一个单元格,它运行正常,所以,这不是公式语法的问题。如何在保存之前强制执行公式?我试过了:

\PHPExcel_Calculation::getInstance($objPHPExcel)->disableCalculationCache();
\PHPExcel_Calculation::getInstance($objPHPExcel)->clearCalculationCache();

没有成功...

php phpexcel
3个回答
12
投票

这是解决问题的示例代码:

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

2
投票

我添加了这段代码,它对我有用。

PHPExcel_Calculation::getInstance($excelObj)->disableCalculationCache();
PHPExcel_Calculation::getInstance($excelObj)->clearCalculationCache();
$writer = PHPExcel_IOFactory::createWriter($excelObj, 'Excel2007');

$writer->setPreCalculateFormulas(true);
$writer->save($output_file);

0
投票

对于那些使用Laravel Excel的人,您可以在excel.php配置文件中轻松设置导出期间的计算。只需将选项calculate设置为true即可。

/*
|--------------------------------------------------------------------------
| Pre-calculate formulas during export
|--------------------------------------------------------------------------
*/
'calculate'                   => true,
© www.soinside.com 2019 - 2024. All rights reserved.