public void ExportToExcel()
{
try
{
this.OrderGridView.AllowPaging = false;
this.OrderGridView.AllowSorting = false;
this.OrderGridView.EditIndex = -1;
this.RefreshGrid();
OrderGridView.Columns.RemoveAt(1);
OrderGridView.Columns.RemoveAt(0);
OrderGridView.DataBind();
Response.Clear();
Response.ContentType = "application/vnd.xls";
Response.AddHeader("content-disposition",
"attachment;filename=MyOrder.xls");
Response.Charset = "";
StringWriter swriter = new StringWriter();
HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
OrderGridView.RenderControl(hwriter);
Response.Write(swriter.ToString());
Response.Flush();
Response.End();
}
catch (Exception exe)
{
throw exe;
}
}
And then call the function on button click:
protected void btnExcel_Click(object sender, ImageClickEventArgs e)
{
ExportToExcel();
this.OrderGridView.AllowPaging = true;
this.OrderGridView.AllowSorting = true;
this.OrderGridView.EditIndex = -1;
this.RefreshGrid();
}
I hope it helps you!