Send a GridView to Excel when a button is clicked.

Add EnableEventValidation="false" to <%@ Page %> tag.

protected void Button1_Click(object sender, System.EventArgs e)
{
 Response.Clear();
 Response.AddHeader("content-disposition", "attachment; filename=FileName.xls");
 Response.Charset = "";
 // If you want the option to open the Excel file without saving than
 // comment out the line below
 // 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);
 GridView1.RenderControl(htmlWrite);
 Response.Write(stringWrite.ToString());
 Response.End();
}

No comments:

Post a Comment