private void ExportGridToExcel(GridView grdGridView, string fileName)
{
Response.Clear();
Response.AddHeader("content-disposition",
string.Format("attachment;filename={0}.xlsx", fileName));
Response.Charset = "";
Response.ContentType = "application/vnd.xlsx";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
grdGridView.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
//You have to override this method
public override void VerifyRenderingInServerForm(Control control)
{
}
<pre lang="c#">