I’ve been banging my head on the wall over this issue for a couple days now and I need some help.
I’m creating an excel file using ASP.net and everything is going just fine except one column has to be formatted as a decimal to 2 places but when it pulls into excel, excel automatically formats it as a whole number instead of keeping the decimal places.
sw.Write(String.Format("{0:f}", CDbl(dr(14)).ToString("0.00"))) this produces “20.00”
but when I open it in excel it’s displayed as 20…if I select the whole column and format it as a number it gets displayed as 20.00 like it’s supposed to but I don’t want to have to do this (I can’t do that) the file is supposed to be automatically picked up and imported into another system that needs the column to be a decimal.
Any suggestions would be greatly appreciated.
Are you writing the output to CSV, HTML, XML, DOCX or are you using a 3rd party library to generate binary Excel 2003?
The answer varies for each.
After formating the excel document, save in the appropriate format (html, CSV, XML, etc) open the file in notepad and look at what formating codes excel sprinkled around your number.
Part 2 (I couldn’t put this in a comment, so I putting my comment in my answer)
For text files (which have no metadata) Excel looks at the first 10 or so rows and makes a guess about the data type and applies a default format. Sometimes you can guide the guessing process by making sure there is data in the first few rows and that it is formated in a way that excel can guess the data type. However, AFAIK, you can’t set the format.
I recommend switching to exporting HTML tables. Excel understands HTML tables and you can use a well documented model (HTML!) to format the results for color, size, etc and probably number formating as long as excel interprets the column as a string and preserves the formating.
If that fails use a 3rd party library such as http://www.codeplex.com/ExcelPackage to export docx.