The getValue () method does exactly what it should do without trying to be smart. It returns the actual value stored in the cell. If this 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 smart returns this value as a formatted string (with zero leading zeros, if necessary), then you need to use a completely different method and apply 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 the formula:
foreach($cellIterator as $cell){ $cells[] = PHPExcel_Style_NumberFormat::toFormattedString( $cell->getCalculatedValue(), $objPHPExcel->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode() ); }
And please do not use @ to try to suppress errors. PHPExcel throws exceptions, and you really should want to catch them.
However, for what you are doing, you can consider the toArray () method of the worksheet, which will return an array of all the cell values ββin the worksheet.
Mark baker
source share