views:

85

answers:

1

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?

+1  A: 

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.

ax
I don't always find the Pear documentation as clear as it could be, but through trial and error playing with the setNumFormat I was able to fix it.
Michael Itzoe