Table导出为xls,doc,txt,htm方法

   经常遇到将DataTable中的数据导到一个excel或者doc等常用文件格式的情况,google了一些资料,总结一下:
将DataTable中的内容写成HTML,Table格式,然后用下面函数,导出。
 1 None.gif private   void  ExportExcel( string  filename,  string  table)  // table为DataTable写成HTML格式的字符串。
 2 ExpandedBlockStart.gif    {
 3InBlock.gif   Response.Clear();
 4InBlock.gif   Response.Buffer = true;
 5InBlock.gif   Response.Charset = "utf-8"//定义文档类型、字符编码
 6InBlock.gif   //attachment 参数表示作为附件下载, 可以改成 online 在线打开
 7InBlock.gif   //filename = 指定输出文件的名称, 注意其扩展名和指定文件类型相符, 可为:.doc  .xls  .txt .htm
 8InBlock.gif   Response.AppendHeader("Content-Disposition","attachment;filename=" + filename + ".xls");
 9InBlock.gif   Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
10InBlock.gif   //Response.ContentType 可以为 application/ms-excel  application/ms-word  application/ms-txt  application/ms-html  或其他浏览器可直接支持文档 
11InBlock.gif   Response.ContentType = "application/ms-excel";
12InBlock.gif   this.EnableViewState = false;
13InBlock.gif    StringWriter oStringWriter = new System.IO.StringWriter();  //定义一个输入流
14InBlock.gif   HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
15InBlock.gif     this.RenderControl(oHtmlTextWriter);  //将目标数据绑定到输入流输出
16InBlock.gif   //this 表示输出本页, 也可以绑定 datagrid, 或其他支持 obj.RenderControl() 属性的控件
17InBlock.gif   Response.Write(table);
18InBlock.gif   Response.End();
19ExpandedBlockEnd.gif  }

转载于:https://www.cnblogs.com/zjy/archive/2006/05/18/402991.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值