try this code i think it will help you....
but one thing i clear that if your have put your gridview inside UpdatePanel Then any of your code will not work and its give the error like this...
Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
put your grid out side the UpdatePanel and use this code it will work for me...
protected void Export2Excel()
{
try
{
this.GridView1.AllowPaging = false;
this.GridView1.AllowSorting = false;
this.GridView1.EditIndex = -1;
this.BindData();
Response.Clear();
Response.ContentType = "application/vnd.xls";
Response.AddHeader("content-disposition",
"attachment;filename=MyList.xls");
Response.Charset = "";
StringWriter swriter = new StringWriter();
HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
GridView1.RenderControl(hwriter);
Response.Write(swriter.ToString());
Response.End();
}
catch (Exception exe)
{
throw exe;
}
}