Php – Problem reading numbers from excel with PHPExcel

PHPphpexcel

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 ++;

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.

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

Best Answer

Or maybe just try ini_set("precision", "15"); as default value is 12. Worked for me.

Related Topic