Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cell formats in phpexcel

Tags:

php

phpexcel

i'm trying to read some excel files with phpexcel, which works ok so far. phpexcel is a bit too clever though, and removes leading zeros from my cells. i guess i need to tell it to treat the cell as a text string, not as general or number. but i've failed. i even found threads on stackoverflow about this but the suggested solutions simply wouldn't work.

below is a snippet of the stuff i'm working on.

foreach(@$objWorksheet->getRowIterator() as $row){
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);
    foreach($cellIterator as $cell){
        #$objWorksheet->getStyle($cell->getCoordinate())->getNumberFormat()->setFormatCode('@');
        $cells[] = $cell->getValue();
    }
}

any idea? i don't want to limit myself to only read numeric content, as i don't have any control over what the users will upload. treating everything as strings would be perfect.

/peder

like image 886
fjallstrom Avatar asked Nov 21 '10 17:11

fjallstrom


2 Answers

The getValue() method does exactly what it should do, without trying to be clever. It returns the actual value stored in the cell. If that cell contains an integer, it returns an integer; if the cell contains a float, it returns a float; if it contains a string, it returns a string. Being clever is returning that value as a formatted string (with leading zeroes if appropriate), then you need to use a rather different method, and apply the cell formatting to the returned value.

foreach($cellIterator as $cell){ 
   $cells[] = PHPExcel_Style_NumberFormat::toFormattedString( $cell->getValue(),
              $objPHPExcel->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode()
   );
}

or if the cell contains a formula:

foreach($cellIterator as $cell){ 
   $cells[] = PHPExcel_Style_NumberFormat::toFormattedString( $cell->getCalculatedValue(),
              $objPHPExcel->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode()
   );
}

And please don't use @ to try and suppress errors. PHPExcel throws exceptions, and you really should want to trap these.

However, for what you're doing, you might consider the worksheet's toArray() method, that will return an array of all the cell values in the worksheet.

like image 131
Mark Baker Avatar answered Sep 28 '22 20:09

Mark Baker


There is something easier than those Iterators. For doing foreach You can also use toArray method, in example:

$active_sheet = $objPHPExcel -> getActiveSheet();
foreach($active_sheet -> toArray() as $row_n => $row){
 foreach($row as $cell_n => $cell){
  // operations here
 }
}

Worked fine for me and also seems to be faster then Iterators, just as Mark Baker sad.

In processing CVS I see one main problem: user have to export data form Excel and this process can be confusing for most of them. That's why i searched for solution for import straight from Excel.

In usage of those iterators is something strange for me and I really didn't get into it much. While accessing data by using iterator it returns few (form 2 to 4 in my case) serialized objects and getting data out of them was nightmare.

like image 37
The Pueblo Avatar answered Sep 28 '22 21:09

The Pueblo