1

我在一个项目中,我正在从一个 excel 文档中读取值。使用 getCalculatedValue 函数时,它总是返回相同的值。我尝试了 getOldCalculatedValue 函数但没有成功。我在某处读到,如果您在重新计算该单元格的值之前清除缓存,将会很有帮助。有人可以用下面的代码解释我怎么能做到这一点吗?

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');  
$objPHPExcel = $objReader->load("test.xlsx");
$value1 = $objPHPExcel->getActiveSheet()->getCell('B1')->getCalculatedValue();
$value2 = $objPHPExcel->getActiveSheet()->getCell('D1')->getCalculatedValue();
4

1 回答 1

1

When you first retrieve a calculated value with a call to getCalculatedValue(), PHPExcel (by default) caches the result, so subsequent calls won't recalculate, but simply return the cached value.

This reduces the calculation overhead typically found when the same calculated cell is referenced in other cell formulae


It can be a problem when you're reading calculated data, then changing cell values and then re-calculating, so PHPExcel provides methods that allow you to control the cache

You can change the default behaviour so that calculated results are never cached, by disabling the calculation cache using either:

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

or:

PHPExcel_Calculation::getInstance($objPHPExcel)->setCalculationCacheEnabled(false);

Or you can flush the cache at any point by making a call to:

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

Note that the getOldCalculatedValue() method is used to retrieve the last value calculated for a cell by MS Excel itself, not from within PHPExcel. It isn't guaranteed to reflect the current data in any way, because it's possible to disable MS Excel from executing formula calculations; but can sometimes be useful for formulae that reference external files or data sources.

于 2015-07-09T14:14:38.607 回答