php 如何使用PHPExcel自动读取计算值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4584963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:34:10  来源:igfitidea点击:

How to automatically read in calculated values with PHPExcel?

phpphpexcel

提问by Edward Tanguay

I have the following Excelfile:

我有以下Excel文件:

alt text

替代文字

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

我通过遍历每个单元格并使用以下方法获取值来读入它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:

然而,虽然它读入数据很好,但它从字面上读入计算:

alt text

替代文字

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

我从讨论中明白像这样的,我可以使用getCalculatedValue()的计算单元。

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

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

Is there a way for me to read in the value of a cell in a way that automatically gets the value if it has a simple value and gets the result of the calculation if it is a calculation?

有没有办法让我以一种方式读取单元格的值,如果它有一个简单的值,则自动获取该值,如果它是一个计算,则获取计算的结果?

Answer:

回答:

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:

事实证明这getCalculatedValue()适用于所有单元格,让我想知道为什么这不是默认值,getValue()因为我认为人们通常需要计算的值而不是方程本身,无论如何这都有效:

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

alt text

替代文字

回答by Edward Tanguay

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

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

回答by pancy1

If you are unsure about the content of a cell (value or formula included), I recommend you to primarily do a check if the cell has a formula and then copy - paste accordingly. getOldCalculatedValue() is very helpful in this case. Here is an example of that:

如果您不确定单元格的内容(包括值或公式),我建议您首先检查单元格是否有公式,然后相应地复制粘贴。getOldCalculatedValue() 在这种情况下非常有用。这是一个例子:

$code = $sheet->getCell('A'.$y)->getValue();
if(strstr($code,'=')==true)
{
    $code = $sheet->getCell('A'.$y)->getOldCalculatedValue();
}
$objPHPExcel4->setActiveSheetIndex(0)
             ->setCellValue('A'.$l, $code);

For large data sets, getCalculatedValue() function is really cumbersome and lots of memory will be required to perform correctly.

对于大型数据集,getCalculatedValue() 函数非常麻烦,并且需要大量内存才能正确执行。

回答by J-who

Looks like getCalculatedValue() is deprecated. Try using getFormattedValue() instead.

看起来 getCalculatedValue() 已被弃用。尝试使用 getFormattedValue() 代替。

回答by user2244112

getCalculatedValue()seems to do the right job you wanted. It will return the correct value if the cell contains FBV ( formula based value ). If not then the normal value will be returned instead.

getCalculatedValue()似乎做你想要的正确的工作。如果单元格包含 FBV(基于公式的值),它将返回正确的值。如果不是,则将返回正常值。

回答by JoeOD

I have never imported an excel file in PHP so this is just a stab in the dark.

我从未在 PHP 中导入过 excel 文件,所以这只是黑暗中的一个刺。

Why not check the first character in the cell for an "="

为什么不检查单元格中的第一个字符是否为“=”

If true getCalculatedValue()
if not getCell()

如果为 true getCalculatedValue()
如果不是 getCell()