Php excel formula not working in saved spreadsheet

I am using php excel codeplex, I populate the cell as follows:

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

I successfully save the file when I open it, there is a formula there, but it does not display the calculated value. If I copy and paste the formula into another cell, it works fine, so this is not a problem with the formula syntax. How to force a formula to execute it before saving? I tried:

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

Without success ...

+4
source share
3 answers

This is 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);
+11
source

I added this code and it works 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);
+2
source

, Laravel Excel, excel.php excel.php. calculate true.

/*
|--------------------------------------------------------------------------
| Pre-calculate formulas during export
|--------------------------------------------------------------------------
*/
'calculate'                   => true,
0

Source: https://habr.com/ru/post/1568174/


All Articles