I'm using php excel codeplex, I'm filling a cell like this:
$objWorksheet->setCellValue('B12', "='Other sheet'!D38");
I save the file successfully, when I open it the formula is there, but it doesn't render the calculated value. If I copy and paste the formula to another cell it runs ok, so, it's not a problem of the formula's syntax. How can I force the formula to execute before saving? I have tried:
\PHPExcel_Calculation::getInstance($objPHPExcel)->disableCalculationCache();
\PHPExcel_Calculation::getInstance($objPHPExcel)->clearCalculationCache();
Without success...
The most common reason for an Excel formula not calculating is that you have inadvertently activated the Show Formulas mode in a worksheet. To get the formula to display the calculated result, just turn off the Show Formulas mode by doing one of the following: Pressing the Ctrl + ` shortcut, or.
Go to File, then Options, then Formulas to see the same setting options in the Excel Options window. Under the Manual Option, you'll see a checkbox for recalculating the workbook before saving, which is the default setting.
On the Formulas tab, in the Calculation group, click Calculation Options, and then click Automatic.
1. Select the formula cells you need to make them permanent, then press the Ctrl + C keys simultaneously to copy them. Now the formulas in selected cells are replaced with corresponding results immediately.
This is a sample code to solve the problem:
$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);
I added this code and it is working for me.
PHPExcel_Calculation::getInstance($excelObj)->disableCalculationCache();
PHPExcel_Calculation::getInstance($excelObj)->clearCalculationCache();
$writer = PHPExcel_IOFactory::createWriter($excelObj, 'Excel2007');
$writer->setPreCalculateFormulas(true);
$writer->save($output_file);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With