Wednesday, April 22, 2009

Coding for Export Gridview Data into Excel format

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=C:\\temp\\vijay.xls");//if u want to Export into Document replace vijay.xls with vijay.doc
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vijay.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

Note:

This method is used to Perform the rendercontrol event.

Add the below function with the coding


public override void VerifyRenderingInServerForm(Control control)
{
}