dongxie3963 2012-11-14 12:42
浏览 117
已采纳

PHPExcel解析.XLS文件,其中“数字存储为文本”警告返回#VALUE

My code uses the getFormattedValue() method to pull the data into PHP.

Here is the problem part of .xls with "numbers stored as text" at column D:

Excel cells contains next values:   D7 -> 0,43;  E7 -> =D7*1,2;  F7 -> =E7*11,2;
getFormattedValue() returns:          0,43;        #VALUE!;          #VALUE!;
getCalculatedValue() also returns     0,43;        #VALUE!;          #VALUE!;
getValue() returns                    0,43;        D7*1.2;           E7*11.2;

If I fix it in excel, as it propose, to number. then getFormattedValue() work just fine. But I can't tell to client that your xls is wrong...

So question, is it possible get PHPExcel to act like Excel? Or maybe some other method to make this work.

Thank you for your help and time!

--- EDIT 1 ---

Basicly it's a comment to Mark Baker's answer.

I would like to find a solution that doesn't include any changes to .xls files. I don't know what structure it would be, where is the problem cells, etc.

Is there some way to make PHPExcel recognize that cells? Maybe how MS Excel do that? Or find out our method to do that.

I think about trying to divide cell value by itself and if we get 1 so that's a numeric cell. AND\OR find cells that are included in mathematical calculations. To better understand what i mean - cell E7 contains D7*1,2. So, we can check - is D7 numeric? Something like that.

At this time I don't have any solution to this problem... So any help would be great.

  • 写回答

1条回答 默认 最新

  • dsqbh42082 2012-11-18 16:49
    关注

    You would need to convert those string values in column D to a valid numeric (using a decimal point rather than a comma) before getting the values from cells in columns E and F. If the problematic cells are always in the same column (or at least predictable) you could use a cell binder to handle this conversion when the workbook is loaded.

    EDIT

    You'd need to create a binder something like:

    class PHPExcel_Cell_AdvancedValueBinder 
        extends PHPExcel_Cell_DefaultValueBinder 
        implements PHPExcel_Cell_IValueBinder
    {
        public function bindValue(PHPExcel_Cell $cell, $value = null)
        {
    
            // sanitize UTF-8 strings
            if (is_string($value)) {
                $value = PHPExcel_Shared_String::SanitizeUTF8($value);
            }
    
            // Find out data type
            $dataType = parent::dataTypeForValue($value);
    
            if ($dataType === PHPExcel_Cell_DataType::TYPE_STRING && 
                !$value instanceof PHPExcel_RichText) {
                if (preg_match('/^\d+,\d+$/', $value)) {
                    list($w, $d) = explode(',', $value);
                    $value = (float) $w . '.' . $d;
                    $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
                    return true;
                }
            }
    
            // Not bound yet? Use parent...
            return parent::bindValue($cell, $value);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能