当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.Net数据输出到EXCEL表格中

Asp.Net数据输出到EXCEL表格中

2018年04月19日  | 移动技术网IT编程  | 我要评论

南大119碎尸案,命运呼叫转移下载,无锡汽车票查询

一、定义文档类型、字符编码
   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();
四、 这时如果发生"只能在执行 render() 的过程中调用 registerforeventvalidation"的错误提示。
有两种方法可以解决:
1.修改web.config(不推荐)<pages enableeventvalidation ="false" ></pages>
2.直接在导出execl的页面修改 
 总结:本例程在microsoft visual studio .net 2003平台下测试通过,适用于c#和vb,当采用vb的时候将 this 关键字改成 me 。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网