469,572 Members | 1,307 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

Problem exporting datagrid with numeric values to Excel

I am using the following code to write data from a datagrid to an Excel file:
Dim tw As New System.IO.StringWriter
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
dg.RenderControl(hw)
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Response.AppendHeader("content-disposition",
"attachment;filename=test.xls")
Response.Write(txtWriter.ToString)
Response.End()
So far, so good. Creates an Excel spreadsheet very nicely. The problem
occurs when a column contains what appears to be numeric data. Leading
zeroes are being dropped. If the number is too large, the value is being
stored in scientific notation format. Is there any way to get my values to
download to Excel as text? These fields are coming from a SQL database where
they are stored as varchar. Thanks for any help.
Nov 19 '05 #1
1 2118
Hi Nancy,

I think it's an Excel bug. Because all are digits
(including leading 0), it automatically recognizes it as
number. In Excel automation, we can set text format for
specific column first then fill this kind of data. That
works perfectly. But for the datagrid exporting I can't
think any solution.

HTH

Elton Wang
el********@hotmail.com
-----Original Message-----
I am using the following code to write data from a datagrid to an Excel file: Dim tw As New System.IO.StringWriter
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
dg.RenderControl(hw)
Response.ContentType = "application/vnd.ms- excel" Response.Charset = ""
Response.AppendHeader("content-disposition",
"attachment;filename=test.xls")
Response.Write(txtWriter.ToString)
Response.End()
So far, so good. Creates an Excel spreadsheet very nicely. The problemoccurs when a column contains what appears to be numeric data. Leadingzeroes are being dropped. If the number is too large, the value is beingstored in scientific notation format. Is there any way to get my values todownload to Excel as text? These fields are coming from a SQL database wherethey are stored as varchar. Thanks for any help.
.

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Hideyuki | last post: by
reply views Thread by Deepa | last post: by
3 posts views Thread by Bidarkota | last post: by
reply views Thread by kanepart2 | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.