SqlDataAdapter da = new SqlDataAdapter("select * from Sample", con);
DataSet ds = new DataSet();
da.Fill(ds);
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
dgrid.DataSource = ds;
htmlWrite.WriteLine("hello");
dgrid.DataBind();
dgrid.RenderControl(htmlWrite);
Response.Clear();
string str="Student";
Response.AddHeader("content-disposition", "attachment;filename='"+str+"'"+".xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
Response.Write(stringWrite.ToString());
Response.End();
Hope it will helpful for you