Add the Folllowing namespace:
using System.IO;
Write the following lines on the click of the Export button.
protected void btnExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "application/ms-excel";
Response.Charset = "";
Page.EnableViewState = false;
Response.AddHeader("Content-Disposition", "inline;filename=report.xls");
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
//Here grid1 is the name of the GridView Control.
grid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
Enjoy Exporting data from GridView to Excel.
using System.IO;
Write the following lines on the click of the Export button.
protected void btnExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "application/ms-excel";
Response.Charset = "";
Page.EnableViewState = false;
Response.AddHeader("Content-Disposition", "inline;filename=report.xls");
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
//Here grid1 is the name of the GridView Control.
grid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
Enjoy Exporting data from GridView to Excel.