asp.net 导出 Excel
// 将GridView的数据导出到Excel
public void ExportExcel(GridView gv)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=Export.xls");
Response.ContentType = "application/excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
dg.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
StringWriter sw = new StringWriter(myCItrad);
HtmlTextWriter htw = new HtmlTextWriter(sw);
try
{
gv.RenderControl(htw);
Response.Write(sw.ToString());
}
catch
{
ClientScript.RegisterStartupScript(this.GetType(), "Failed", "<script>alert('导出失败!')</script>");
}
finally
{
Response.End();
}
}