protected void Button1_Click(object sender, EventArgs e)
{
this.OutExcel(DataGrid1, "attachment;filename=File2.xls", "application/ms-excel");
}
private void OutExcel(DataGrid dg, string name, string type)
{
dg.Visible = true;
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", name);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = type;
dg.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
dg.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}