Question

In my ASP.NET application, I need to write DataTable as CSV response to the customer. Everything is working fine except a column which has a numbers.

Ex: 7002136138603600000

But when I open the CSV in Excel, it is showing in exponential format.

Something like this: 7E+18

Could you please let me know what needs to be done in order to show these values as test instead in exponential format?

Thanks, Mahesh

Was it helpful?

Solution

Regarding the CSV numbers format, check here: http://support.microsoft.com/kb/214233


I did export to Excel many times. Just write the output of the Gridview to a String Builder using an HtmlTextWriter send the response.

You can find two examples of doing this:

More search results: http://www.google.com/search?q=export+gridview+to+excel

So, I think the easiest way is to bind to GridView and write it as HTML as in the other examples, unless you need CSV for another reason also.

OTHER TIPS

You can't do it with CSV. Try it with Excel. Create a spreadsheet with the value "7002136138603600000" in a cell, preceded by a single quote (') to indicate that it's text. Excel will display the full number correctly. Save it as CSV. Reload. The number will be displayed using exponential format.

Alternatively you can use HTML. Add the namespace xmlns:x="urn:schemas-microsoft-com:office:excel" to your html root and at this attribute to your td element.

x:str="'7002136138603600000"

Excel will display it as text. For a more complete example create the Excel doc like I mentioned and save as html.

You can also use XML if you know your customers are using Excel 2003 or newer.

Open up the file in a text editor and you will likely find that the numbers are exporting correctly. Excel simply defaults to exponential format for some long numbers.

Does it HAVE to be CSV? If so, then you're pretty much stuck with the default behavior from Excel.

If it doesn't have to be CSV, then look into the Excel XML format, which will give you much finer control over the display formats (anything that can be done within Excel can be done in the XML file string). Of course, this is only supported in Office 2003 and later.

its easy add asc char 32 when the numbers are all digits, use a function to find out if the numbers are all digits and if true, then simple add asi 32 to the number (convert to string if required). Tested csv file on microsoft excel 2007 and windows 7 and it works fine, not sure about earlier versions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top