Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem reading numbers from excel with PHPExcel

Tags:

php

phpexcel

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

like image 658
Tjirp Avatar asked Apr 09 '26 18:04

Tjirp


2 Answers

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

like image 95
T.O. Avatar answered Apr 11 '26 17:04

T.O.


What platform are you on?

On my Mac, using the (64 bit) PHP interpreter from MacPorts with PHPExcel 1.7.6, I seem to get the correct values:

array (
  1 => 
  array (
    0 => 4078500264822,
    1 => '02648-32.000.00',
    2 => 'Wand-slangenbox automatic',
    3 => '20m slang, rolt zichzelf gelijkmatig op na gebruik. Geïntegreerde draaggreep voor 
comfortabel transport, inclusief montagemateriaal en wand-/vloerhouder.',
    4 => 17400,
    5 => 119.95,
    6 => '',
    7 => 12,
  ),
  2 => 
  array (
    0 => '4078500888707',
    1 => '08887-20.000.00',
    2 => 'Accu gras-buxusschaar set',
    3 => 'Eenvoudig gras en buxus in vorm knippen zonder storende kabels. De messen kunnen worden verwisseld zonder gereedschap, het is bijzonder gemakkelijk, snel en veilig. Laat tevens op tijd zien wanneer de accu weer moet worden opgeladen. ',
    4 => 16340,
    5 => 69.95,
    6 => 79.95,
    7 => 12,
  ),
)

What is the output when you run:

$ php -r 'echo PHP_INT_MAX;'

If it's 2147483647, then try a 64 bit interpreter. Or file a request with PHPExcel to fix the casting.

like image 42
crishoj Avatar answered Apr 11 '26 18:04

crishoj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!