Asp.Net页面输出到EXCEL

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 xk2009 的 blog

其实,利用asp.net输出指定内容的word、excel、txt、htm等类型的文档很容易的。主要分为三步来完成。 

一、定义文档类型、字符编码   

   response.clear(); 

   response.buffer= true; 

   response.charset="utf-8";   

   //下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开 

   //filename=fileflow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc    .xls    .txt   .htm   

   response.appendheader("content-disposition","attachment;filename=fileflow.xls"); 

   response.contentencoding=system.text.encoding.getencoding("utf-8");   

   //response.contenttype指定文件类型 可以为application/ms-excel    application/ms-word    application/ms-txt    application/ms-html    或其他浏览器可直接支持文档  

   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.rendercontrol(ohtmltextwriter);    

   //this 表示输出本页,你也可以绑定datagrid,或其他支持obj.rendercontrol()属性的控件   

   response.write(ostringwriter.tostring()); 

   response.end();   

  总结:本例程在microsoft visual studio .net 2003平台下测试通过,适用于c#和vb,当采用vb的时候将 this 关键字改成 me 。 

本文关键:Asp.Net页面输出到EXCEL
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top