简体   繁体   English

PHPExcel无法读取XML单元格中的完整数字

[英]PHPExcel not reading complete number in XML cell

I must extract data from a third party XML file. 我必须从第三方XML文件中提取数据。 All is fine expect for one column that contains numbers with comma thousand separator,and dot decimal separator. 对于包含以逗号分隔的数字和小数点分隔的数字的一列,一切都很好。

A number normally represented as "10,000.00" in Excel or OpenOffice, is read by PHPExcel as "10" only. 在Excel或OpenOffice中通常表示为“ 10,000.00”的数字,PHPExcel仅将其读取为“ 10”。

require_once dirname(__FILE__) . '/../Classes/PHPExcel/IOFactory.php';

$inputFileName = "myfile.xml";

/**  Identify the type of $inputFileName  **/
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);

/**  Create a new Reader of the type that has been identified  **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);

/**  Load $inputFileName to a PHPExcel Object  **/
$objPHPExcel = $objReader->load($inputFileName);

$format = $objPHPExcel->getActiveSheet()->getStyle('C24')->getNumberFormat()->getFormatCode();

Note that format code of the cell is returned as General 请注意,单元格的格式代码将作为“常规”返回

Trying to read one of the cell causing problems using: 尝试使用以下方法读取其中一个引起问题的单元格:

$getValue = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(2, 24)->getValue();
$getCalculatedValue = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(2, 24)->getCalculatedValue();
$getFormattedValue = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(2, 24)->getFormattedValue();

Always the same value, truncated at the thousand separator: "10" instead of "10,000.00" 始终相同的值,在千位分隔符处截断:“ 10”而不是“ 10,000.00”

Here's the details of the xml file: 这是xml文件的详细信息:

`<?xml version="1.0" encoding="UTF-8"?>
<?mso-application progid="Excel.Sheet"?>
<ss:Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ext="http://fxcm.com/xslt/extension" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:of="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40">`

STYLE DEFINITION 样式定义

`<ss:Style ss:ID="cell_merged_top_odd_integer" ss:Parent="cell_merged_top_odd">
            <ss:NumberFormat ss:Format="#,##0"/>
</ss:Style>`

CELL WITH NUMBER CAUSING PROBLEM 带有数字起因问题的单元格

`<ss:Cell ss:StyleID="cell_merged_top_odd_integer" ss:Index="3">
    <ss:Data ss:Type="Number">10,000.00</ss:Data>
</ss:Cell>`

MANUAL WAY TO FIX THE PROBLEM When I save the file from OpenOffice or Excel, the following message pops up warning message OpenOffice , and PHPExcel finally works as expected, reading "10000". 解决问题的手动方法当我从OpenOffice或Excel保存文件时,以下消息弹出警告消息OpenOffice ,PHPExcel终于按预期方式工作,显示为“ 10000”。

I cannot act on the third party to change its XML file, nor can I ask the user to re-save the file prior to uploading it on my website. 我不能代表第三方更改其XML文件,也不能要求用户在将文件上传到我的网站之前重新保存该文件。

Any idea of what could possibly go wrong here perhaps? 对这里可能出什么问题的任何想法吗?

warning message OpenOffice 警告消息OpenOffice

As suggested by Mark Baker, I took the problem at the source, went through the process of PHPExcel in order to str_replace the comma. 正如Mark Ba​​ker所建议的那样,我从源头上解决了这个问题,并通过PHPExcel进行了处理,以便str_replace逗号。

In the file Excel2003XML.php (the first library called after identifying the file type), I have modified the public function loadIntoExisting() as follows: 在文件Excel2003XML.php(标识文件类型后调用的第一个库)中,我对公共函数loadIntoExisting()进行了如下修改:

public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
    {
    ...
    if (isset($cell->Data)) {
       $cellValue = $cellData = $cell->Data;
       //REPLACE THE COMMA
       $cellValue = str_replace(",", "", $cellValue);
    ...
    }
}

I am conscious that this will affect all cells containing a comma but so far it fixes the problem. 我意识到这会影响所有包含逗号的单元格,但到目前为止,它已解决了该问题。 I'll be looking in replacing the comma only when a specific number format is detected later on. 我将只在以后检测到特定数字格式时才替换逗号。

Meanwhile, thank you Mark for your guidance! 同时,谢谢Mark的指导!

Not surprising that PHPExcel reads only 10 when reading a number that actually contains a string value of 10,000.00 The ss:Data for a ss:Type of Number should contain a numeric value, not a formatted string; 毫不奇怪,PHPExcel只读取10读取一个数字,实际上包含的字符串值时10,000.00ss:Datass:TypeNumber应该包含一个数值,而不是一个格式化字符串; ie. 即。 it should contain an actual numeric value of 10000 , and the ss:Index should point to the formatting... Because the XML has said that the value is numeric, then PHPExcel is trying to read it as a numeric, using standard loose-typing rules; 它应该包含一个实际的数字值10000 ,并且ss:Index应该指向格式...因为XML表示该值是数字,所以PHPExcel尝试使用标准的松散类型将其读取为数字。规则 and as , is not a numeric value, it's only reading the digits up to that non-numeric character 并且,因为,不是数字值,所以它仅读取直到该非数字字符的数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM