Reading cells as strings (leading zero problem)

Aug 3, 2011 at 4:02 PM

Hi there, 

Firstly thank you for such a comprehensive interface to excel reading and writing in PHP ;) 

I have been lurking for an answer to a problem about reading excel data but have not found a solution 

that works for me. I have a spread sheet with cells that contain either dates, text or numbers (with leading zeros) and 

I would like to loop through the spread sheet and place the values in a database. However I cannot get the code 

to either 1) Recognize a cell as a date AND  2) Parse a cell to retain the leading zeros.

Would anyone be able to advise?


Many thanks ,


Some pseudo(ish) code :  


foreach ($workSheet->getRowIterator() as $row) {

            $cellIterator = $row->getCellIterator();  

            foreach ($cellIterator as $cell) {    

                         if (PHPExcel_Shared_Date::isDateTime($workSheet->getCell($cell->getCoordinate() ) ) ){ 

                                    echo "Date value ";

                                     $data = PHPExcel_Style_NumberFormat::toFormattedString($cell->getValue(),'dd/mm/YYYY');


                                       $data = $cell->getValueAsStringAndKeepLeadingZeros();  





Aug 22, 2011 at 12:51 PM

Hi there, 

If this is not possible, please will you let me know. Otherwise I may spend too much time trying to make it work.