PHPExcel getCalculatedValue() 导致 #REF

发布于 2025-01-02 18:43:49 字数 341 浏览 6 评论 0原文

我在 PHPExcel 中使用 getCalculatedValue() 来读取 34 个 Excel 文件。不幸的是,有些用户将 i:\drive\test[month.xls]$C$1 放入单元格中,导致 PHPExcel 无法读取和存储 #REF!在mysql中。

我知道 getCalculatedValue() 将读取单元格中的公式,但是链接怎么样?另外,Excel 文件中找到的链接是外部链接,我无法访问。

尽管单元格包含指向另一个工作表的链接,但单元格本身显示一些值。最好的解决方案是复制全部并粘贴值。但为此目的打开 34 个 Excel 文件似乎不切实际。

我很感激有人能启发我。

谢谢。

I'm using getCalculatedValue() in my PHPExcel to read 34 Excel files. Unfortunately there were some users who put i:\drive\test[month.xls]$C$1 in the cell causing PHPExcel unable to read and store #REF! in the mysql.

I know that getCalculatedValue() will read formula in a cell, but how about link? Somemore, the link found in the Excel files are external link, which I couldn't access.

Although the cell contain links to another worksheet, but on the cell itself displaying some value. The best solution is to copy all and paste value. But this seem not practical to open 34 Excel files for this purpose.

I appreciate someone can enlighten me.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你是我的挚爱i 2025-01-09 18:43:49

PHPExcel 的最新 SVN 代码如果遇到对外部工作簿的引用,将会抛出异常。您可以将 getCalculatedValue() 调用包装在 try/catch 块中,如果引发此异常,请使用 getOldCalculatedValue() 来检索 MS Excel 本身上次计算此单元格值时的结果。

#REF!响应可能是公式的完全有效响应,因此仅仅捕获它是不够的,这就是在这种特定情况下抛出异常的原因。

请注意,这并不能得到保证 - 可能会在 Excel 中禁用自动计算,或者引用的外部工作簿在保存您正在阅读的工作簿后已被修改 - 在这两种情况下, getOldCalculatedValue() 可能会返回错误的结果,你无法知道情况是否如此......但它通常是相当准确的。 SVN 代码的修改正是出于这个原因,当时我在 12 月份工作的一个客户也遇到了同样的情况;该代码将包含在 PHPExcel 1.7.7 生产版本中,该版本将于本周末发布。

编辑

try {
    $calculatedValue = $objPHPExcel->getActiveSheet()->getCell('A1')->getCalculatedValue();
} catch (Exception $e) {
    $calculatedValue = $objPHPExcel->getActiveSheet()->getCell('A1')->getOldCalculatedValue();
}

The latest SVN code for PHPExcel will throw an exception if it encounters a reference to an external workbook. You can wrap your getCalculatedValue() call in a try/catch block, and if this exception is thrown use getOldCalculatedValue() to retrieve the result from when MS Excel itself last calculated this cell's value.

A #REF! response may be a perfectly valid response from a formula, so it isn't adequate simply to trap for that, which is why the Exception is thrown in this particular circumstance.

Note that this is not guaranteed - it is possible to disable automatic calculation in Excel, or for the referenced external workbooks to have been modified since the workbook you are reading was saved - in both these cases, getOldCalculatedValue() may return an erroneous result, and you have no way of knowing if this is the case... but it's generally pretty accurate. The SVN code modifications were for precisely this reason, when I had the same situation for a client that I was working with back in December; and the code will be included within the 1.7.7 production release of PHPExcel that should be available by the end of this week.

EDIT

try {
    $calculatedValue = $objPHPExcel->getActiveSheet()->getCell('A1')->getCalculatedValue();
} catch (Exception $e) {
    $calculatedValue = $objPHPExcel->getActiveSheet()->getCell('A1')->getOldCalculatedValue();
}
无声情话 2025-01-09 18:43:49

好的,完成了,但是解决方案不是那么干净整洁。我使用的是 Try 方法,代码如下:

try{
if($objPHPExcel->setActiveSheetIndexByName($caRef)->getCell($cells)->getCalculatedValue() == '#REF!' || $objPHPExcel->setActiveSheetIndexByName($caRef)->getCell($cells)->getCalculatedValue() == '#VALUE!')
{
    $value = $objPHPExcel->setActiveSheetIndexByName($caRef)->getCell($cells)->getOldCalculatedValue();
}
    else
{
    $value = $objPHPExcel->setActiveSheetIndexByName($caRef)->getCell($cells)->getCalculatedValue();
}

}

如您所见,我没有使用 Catch 和 Throw,因为我真的希望 PHPExcel 抓取该值并将其放入 MySQl 中。但是,这个虽然脚本不整洁,但确实有效。

Ok done, but the solution is not that clean and neat. I'm using Try method, which the code can be seen as follows:

try{
if($objPHPExcel->setActiveSheetIndexByName($caRef)->getCell($cells)->getCalculatedValue() == '#REF!' || $objPHPExcel->setActiveSheetIndexByName($caRef)->getCell($cells)->getCalculatedValue() == '#VALUE!')
{
    $value = $objPHPExcel->setActiveSheetIndexByName($caRef)->getCell($cells)->getOldCalculatedValue();
}
    else
{
    $value = $objPHPExcel->setActiveSheetIndexByName($caRef)->getCell($cells)->getCalculatedValue();
}

}

As you can see, I'm not using Catch and Throw, as I really want PHPExcel to grab the value and put it in the MySQl.However, this really working although is not neat and tidy script.

别想她 2025-01-09 18:43:49

@Mark Ba​​ker 非常感谢您的回答。它对于在 Excel 文件中找到的链接非常有用,是外部链接。对我来说是额外的。

$cell = $activeSheet->getCellByColumnAndRow($colIndex,$rowIndex)->getValue();
                    if($cell instanceof PHPExcel_RichText)     //richText with color etc 
                        $cell = $cell->__toString();  
                    if(substr($cell,0,1)=='='){ //with fomula
                        try{
                            if($activeSheet->getCellByColumnAndRow($colIndex,$rowIndex)->getCalculatedValue() == '#REF!' || $activeSheet->getCellByColumnAndRow($colIndex,$rowIndex)->getCalculatedValue() == '#VALUE!')
                            {
                                $cell = $activeSheet->getCellByColumnAndRow($colIndex,$rowIndex)->getOldCalculatedValue();
                            }
                                else
                            {
                                $cell= $activeSheet->getCellByColumnAndRow($colIndex,$rowIndex)->getCalculatedValue();
                            }
                        }catch(Exception $e){
                            $cell = $activeSheet->getCellByColumnAndRow($colIndex,$rowIndex)->getOldCalculatedValue();
                        }
}

我希望它会有用!

@Mark Baker thanks so much for the answer. It's real useful for the link found in the Excel files are external link.addtional for myself.

$cell = $activeSheet->getCellByColumnAndRow($colIndex,$rowIndex)->getValue();
                    if($cell instanceof PHPExcel_RichText)     //richText with color etc 
                        $cell = $cell->__toString();  
                    if(substr($cell,0,1)=='='){ //with fomula
                        try{
                            if($activeSheet->getCellByColumnAndRow($colIndex,$rowIndex)->getCalculatedValue() == '#REF!' || $activeSheet->getCellByColumnAndRow($colIndex,$rowIndex)->getCalculatedValue() == '#VALUE!')
                            {
                                $cell = $activeSheet->getCellByColumnAndRow($colIndex,$rowIndex)->getOldCalculatedValue();
                            }
                                else
                            {
                                $cell= $activeSheet->getCellByColumnAndRow($colIndex,$rowIndex)->getCalculatedValue();
                            }
                        }catch(Exception $e){
                            $cell = $activeSheet->getCellByColumnAndRow($colIndex,$rowIndex)->getOldCalculatedValue();
                        }
}

I hope it will by useful!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文