Leading zeros being dropped from CSV files

Topics: Developer Forum
Jan 7, 2013 at 12:08 PM

PHPExcel: 1.7.8, 2012-10-12


I currently am using PHPExcel to read from an uploaded file, with xls, xlsx and csv being allowed upload formats. One of the cells in the spreadsheet will often contain a number with a leading zero, e.g. 012345. For excel spreadsheet uploads, as long as the uploader has formatted the column as "Text" in Excel, the data is being read fine with the leading zero. However for csv uploads the leading zero is being dropped, regardless of whether the field is enclosed in quotes or not in the csv file.

Is there any way to prevent the zero from being dropped from this field in a CSV upload using PHPExcel? 

Previously I was using the line $exceldoc=PHPExcel_IOFactory::load("$fname"); for both filetypes. I tried using the following for CSV only but it did not prevent the leading zeros from being dropped:

$objReader = new PHPExcel_Reader_CSV();   

$exceldoc = $objReader->load("$fname");

Any help or even a pointer in the right direction would be greatly appeciated.



Jan 7, 2013 at 12:55 PM

Create a value binder that enforces the data being stored as a string rather than converted to a numeric

Jan 9, 2013 at 6:24 PM

Thanks Mark, that worked perfectly for me, helped by your own examples on http://stackoverflow.com/questions/12457610/reading-numbers-as-text-format-with-phpexcel