468,537 Members | 1,682 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Export datagrid to excel

Hi,

I copied the following codes from Internet to export datagrid to Excel, it
works with datagrid not supporting sorting or paging.

Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Response.AddHeader("Content-Disposition", "attachment;filename=Statement.xls")
Me.EnableViewState = False

Dim tw As New System.IO.StringWriter
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
DataGrid1.RenderControl(hw)
Response.Write("<table><tr><td><b>" + lblMessage.Text +
"</b></td></tr><tr><td>" + tw.ToString() + "</td></tr></table>")
Response.End()

Looks like if datagrid supports sorting or paging, ASP.Net will render HTML
with Javascript inside, and the codes here do not like that. Is there a
workaround?

TIA
Nov 19 '05 #1
1 1863
You should disable datagrid paging when exporting to Excel. In that case
just write out all the records in one long page because this is how Excel
likes to deal with data. Excel doesn't do JavaScript.

Here's more info:
http://www.SteveOrr.net/articles/ExcelExport.aspx
http://www.SteveOrr.net/articles/ExportPanel.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"Ed Chiu" <Ed****@discussions.microsoft.com> wrote in message
news:65**********************************@microsof t.com...
Hi,

I copied the following codes from Internet to export datagrid to Excel, it
works with datagrid not supporting sorting or paging.

Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Response.AddHeader("Content-Disposition",
"attachment;filename=Statement.xls")
Me.EnableViewState = False

Dim tw As New System.IO.StringWriter
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
DataGrid1.RenderControl(hw)
Response.Write("<table><tr><td><b>" + lblMessage.Text +
"</b></td></tr><tr><td>" + tw.ToString() + "</td></tr></table>")
Response.End()

Looks like if datagrid supports sorting or paging, ASP.Net will render
HTML
with Javascript inside, and the codes here do not like that. Is there a
workaround?

TIA

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bidarkota | last post: by
6 posts views Thread by Sridhar | last post: by
19 posts views Thread by cj2 | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.