使用PHPExcel从Excel读取数字时出现问题 [英] Problem reading numbers from excel with PHPExcel

查看:724
本文介绍了使用PHPExcel从Excel读取数字时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHPExcel从excel工作表中读取数字.

I am trying to read a number from an excelsheet using PHPExcel.

我具有读取数据的代码:

The code I have that reads the data:

$objReader = PHPExcel_IOFactory::createReaderForFile($upload_file);
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($upload_file);
$objWorksheet = $objPHPExcel->getActiveSheet();
$data = array();

$i = 0;

foreach ($objWorksheet->getRowIterator() as $row) {
    if ($i < 1) {
    $i++; 
    continue; //Skip heading titles
}
$cellIterator = $row->getCellIterator();        
$cellIterator->setIterateOnlyExistingCells(false);      
foreach ($cellIterator as $cell) {          
    $data [$i] [] = $cell->getValue();          
}       
$i ++;

该代码有效.我阅读了Excel,所有数据都很好地保存在$ data数组中. 问题出在A列中,我得到了一个数字(4078500264822).它返回为"4078500264820"(请参阅​​最后一个0).不管我怎么读.如果我打印整个行,我看到值是错误的.当我使用"getCalculatedValue"之类的函数时,结果是相同的.

The code works. I read the Excel and all the data is nicely saved in the $data array. Problem is in Column A I got a number ( 4078500264822 ). Which it returns as "4078500264820" (see the last 0). No matter how I read it. if I print the entire Row i see the value is wrong. when I use functions such as "getCalculatedValue" the result is the same.

我认为这与如何在excel中保存字段有关.我将其保存为数字"类型. 就像在另一行上一样.我保存了相同的号码.这次以文本"形式显示(excel对此发出了通知). PHPExcel可以读取该号码.我有40张记录纸.其中26个单元格的最后一个数字替换为0.您可以在此处找到截断的文件(仅2行)-> http://dl.dropbox.com/u/1458083/excel.xlsx

I think it has something to do with how the field is saved in excel. I have it saved as type "Number". Where as on another row. I have the same number saved. this time as "text" (excel throws a notice about this). PHPExcel can read that number. I have a 40 record sheet. where 26 cells get their last number replaced with a 0. You can find a truncated file (only 2 rows) here --> http://dl.dropbox.com/u/1458083/excel.xlsx

推荐答案

感谢您对crishoj的支持和支持.不幸的是,它没有解决问题;( 我无法为此处的任何人提供正确的解决方案,因为即使在调用Excel load函数之前,我的数据也已被损坏.

Thank you mark and crishoj for your support. Unfortunatly it did not solve the problem ;( I can not give anyone here the correct solution, since my data apperently was corrupted even before the Excel load function was called.

我通过强迫客户使用CSV解决了该问题.这是代码中的一个非常小的更改(又名1行,PHPExcel ==太棒了!),现在我将所有内容都接收为字符串(因此,它可以正常工作!)

I solved the problem by forcing my customer to use CSV. it was a very minor change in the code (aka. 1 line, PHPExcel == awesome!) and now Im recieving everything as a string (and thus, it works!)

再次感谢您的所有帮助.

Again, thank you for all the help.

这篇关于使用PHPExcel从Excel读取数字时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆