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 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退