Can't render Gridview

A

ad

I use the codes below to render a Gridview to Excel.
But it fail with
RegisterForEventValidation can just be call in Render()

But if myGrid is a DataGrid, it run well.

How can I render a GridView to Excel?


----------------------------------------------------------------------------------------
Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
myGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
 
N

Nicholas Paldino [.NET/C# MVP]

This will not work. The reason being is that the Excel file format is
not HTML, which is what you are writing.

You would have to save the HTML, or write XML in the format that Excel
expects (I believe Office 2003 supports sheets in an XML format now).

Or, you could find a third party component that allows you to write XLS
files. I wouldn't advise automating Excel in a web environment.

Hope this helps.
 

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

Top