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
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.
or if the cell contains a formula:
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.