Can't render Gridview

  • Thread starter Thread starter ad
  • Start date Start date
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();
 
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.
 
Back
Top