Leading zeros being dropped from CSV files

Topics: Developer Forum
Jan 7, 2013 at 11:08 AM

PHPExcel: 1.7.8, 2012-10-12

PHP 5

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.

Alan

 

Coordinator
Jan 7, 2013 at 11:55 AM

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

Jan 9, 2013 at 5: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