Search code examples
phpexcelpearexport-to-excel

Pear Excel Writer: trimming leading zeros - how to prevent?


I'm using the Pear Excel Writer package to export a dataset to Excel from PHP. My problem is some of the data fields have leading zeros (such as a zip code), but these are being trimmed in Excel. I tried adding an apostrophe to the start of the fields to force a text field, but then Excel displays it, which I don't want. I'm neither a PHP nor Excel expert, but I expect I'm missing something relatively simple. Any ideas?


Solution

  • to display leading zeros in excel, you have to apply a custom number format. for fixed-length numbers - your case - this would be something like 00000 (for a 5 digit number), for variable-length numbers something like "000"# (a number with 3 leading zeros). this excel format should be taken over by Spreadsheet_Excel_Writer. if not, you could set it from within the writer with Format::setNumFormat.