Monday, July 13, 2009

Export data from datagrid to excel

Response.Clear();
Response.Buffer= true;
Response.AddHeader("content-disposition", "attachment;filename=Filename.xls"); Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel"; // if you change the type to text/xml, you can export the data to xml file
this.EnableViewState = false;
// if you want to add the header for excel report, you can achieve through the following code
objStringWriter.WriteLine("Title for Excel if required");
System.Web.UI.HtmlTextWriter(objStringWriter);
YourDataGrid.RenderControl(objHtmlTextWriter);
Response.Write(objStringWriter.ToString());
Response.End();

HTH

No comments:

Post a Comment