public void csExportToExcel(System.Web.UI.WebControls.DataGrid gridDownload,System.Data.DataSet lds, System.Web.HttpResponse Response,Page myPage,string fileName)
...{
gridDownload.DataSource = lds;
gridDownload.DataBind();
Response.Clear();
Response.Buffer = true;
Response.ContentType="application/vnd.ms-excel";
Response.AddHeader("Content-disposition", "attachment; filename="+fileName+".xls");
Response.ContentEncoding=System.Text.Encoding.Default;
Response.Charset="";
myPage.EnableViewState = false;
StringWriter SW = new StringWriter();
HtmlTextWriter htmlTW = new HtmlTextWriter(SW);
gridDownload.AllowPaging = false;
gridDownload.RenderControl(htmlTW);
Response.Write(SW.ToString());
Response.End();
}
本文介绍了一个使用ASP.NET将DataGrid控件的数据导出为Excel文件的方法。通过设置HTTP响应头并利用StringWriter和HtmlTextWriter来渲染DataGrid为HTML表格,最终输出为Excel文件供用户下载。
1454

被折叠的 条评论
为什么被折叠?



