gvStaff.DataBind();
string filename = "Staff.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = ViewState["ds"];
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel";
string head = "<meta http-equiv='Conten-type' content ='text/html; charset=utf-8'>";
Response.Charset = "UTF-8";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(head+tw.ToString());
Response.End();
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), C#, VS 2010 (.NET 4.x)