其实,利用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 。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器