`
izuoyan
  • 浏览: 8935100 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

页面导出到Excel、Word、txt

阅读更多
  1. 一、定义文档类型、字符编码
  2. Response.Clear();
  3. Response.Buffer=true;
  4. Response.Charset="utf-8";
  5. //下面这行很重要,attachment参数表示作为附件下载,您可以改成online在线打开
  6. //filename=FileFlow.xls指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc||.xls||.txt||.htm
  7. Response.AppendHeader("Content-Disposition","attachment;filename=FileFlow.xls");
  8. Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
  9. //Response.ContentType指定文件类型可以为application/ms-excel||application/ms-word||application/ms-txt||application/ms-Html||或其他浏览器可直接支持文档
  10. Response.ContentType="application/ms-excel";
  11. this.EnableViewState=false;
  12. 二、定义一个输入流
  13. System.IO.StringWriteroStringWriter=newSystem.IO.StringWriter();
  14. System.Web.UI.HtmlTextWriteroHtmlTextWriter=newSystem.Web.UI.HtmlTextWriter(oStringWriter);
  15. 三、将目标数据绑定到输入流输出
  16. this.RenderControl(oHtmlTextWriter);
  17. //this表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
  18. Response.Write(oStringWriter.ToString());
  19. Response.End();
  20. 注意:有控件,有图片就会出错,只能用于导出较简单的网页,如果需要导出的页面中有css文件链接,导出文件时会弹出提示“无法找到xxx.css文件”,因此需要把页面中用到的样式从css样式文件中复制出来,写在页面中。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics