从datagrid导出html代码,生成excel文件,给客户端下载 ,datagrid不能有分页、排序等,否则出错
实现方法:
Response.Clear();
Response.Buffer= false;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=test.xls"
![](images/wink.gif)
;
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"
![](images/wink.gif)
; Response.ContentType = "application/ms-excel"; this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();