如何使用PHPExcel自动读取计算值? [英] How to automatically read in calculated values with PHPExcel?

查看:68
本文介绍了如何使用PHPExcel自动读取计算值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Excel 文件:

我通过在每个单元格上进行循环并使用getCell(...)->getValue()来获取值来读取它:

I read it in by looping over every cell and getting the value with getCell(...)->getValue():

$highestColumnAsLetters = $this->objPHPExcel->setActiveSheetIndex(0)->getHighestColumn(); //e.g. 'AK'
$highestRowNumber = $this->objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$highestColumnAsLetters++;
for ($row = 1; $row < $highestRowNumber + 1; $row++) {
    $dataset = array();
    for ($columnAsLetters = 'A'; $columnAsLetters != $highestColumnAsLetters; $columnAsLetters++) {
        $dataset[] = $this->objPHPExcel->setActiveSheetIndex(0)->getCell($columnAsLetters.$row)->getValue();
        if ($row == 1)
        {
        $this->column_names[] = $columnAsLetters;
        }
    }
    $this->datasets[] = $dataset;
}

但是,尽管它可以很好地读取数据,但它可以从字面上读取计算:

However, although it reads in the data fine, it reads in the calculations literally:

我从讨论中了解像这样,我可以使用getCalculatedValue()用于计算单元格.

I understand from discussions like this one that I can use getCalculatedValue() for calculated cells.

问题在于,在我要导入的Excel工作表中,我事先不知道要计算哪些单元格,哪些不是.

The problem is that in the Excel sheets I am importing, I do not know beforehand which cells are calculated and which are not.

我是否可以通过一种方式来读取单元格的值,如果它具有简单值,则可以自动获取值,如果是计算,则可以自动获取计算结果?

事实证明getCalculatedValue()适用于所有单元格,这使我想知道为什么这不是getValue()的默认值,因为我认为在任何情况下,通常都需要计算的值而不是方程式本身这种情况有效:

It turns out that getCalculatedValue() works for all cells, makes me wonder why this isn't the default for getValue() since I would think one would usually want the value of the calculations instead of the equations themselves, in any case this works:

...->getCell($columnAsLetters.$row)->getCalculatedValue();

推荐答案

getCalculatedValue()似乎适用于所有单元格,请参见上文

getCalculatedValue() seems to work for all cells, see above

这篇关于如何使用PHPExcel自动读取计算值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆