Export datagrid to excel

G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top