Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
DataGrid dg = new DataGrid();
dg.DataSource = GetDataSource(); //返回DataSet或DataTable
dg.DataBind();
dg.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
如果导出有乱码:Response.ContentEncoding = System.Text.Encoding.UTF8
导出Excel
本文介绍了一种使用ASP.NET将数据网格(DataGrid)中的数据导出为Excel文件的方法。具体步骤包括设置HTTP响应头、禁用视图状态、创建DataGrid实例并绑定数据源,最后将DataGrid的内容渲染到字符串中并发送到客户端。
1466

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



