惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
B
Blog
WordPress大学
WordPress大学
Project Zero
Project Zero
P
Palo Alto Networks Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
T
Tailwind CSS Blog
Forbes - Security
Forbes - Security
F
Full Disclosure
SecWiki News
SecWiki News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News: Ask HN
Hacker News: Ask HN
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
O
OpenAI News
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Cybersecurity and Infrastructure Security Agency CISA
S
Securelist
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
IT之家
IT之家
U
Unit 42
腾讯CDC
S
Security Affairs
C
Cisco Blogs
Schneier on Security
Schneier on Security
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss

博客园 - leon1005

如何制作一个有颜色的ListBox,颜色选择下拉列表 获取当前程序集的物理地址 获取当前Url的主机头 获取.config文件节点的一些方法 ASP.NET 中的正则表达式 ITsun 网站流量统计被挂马。。已证实!! 禁止文本框粘贴功能(支持IE、Mozlla、FireFox等) 判断sqlserver的ntext字段是否为空的方法 存储过程里判断大小,然后赋值 请问架构高手.NET2.0的Cache和JAVA的hibernate的缓存是否类似? Cache.Insert的一些参数(.NET 1.1) 用C# 实现TCP 协议功能 Asp.net 2.0新增的缓存管理 StringBuilder类的用法范例 Foreach In(C#) Cache的认识 Request.ServerVariables参数集 遍历浏览文件目录,并存入数据库 PetShop is Evil?
C#导出Word
leon1005 · 2007-09-21 · via 博客园 - leon1005

private void Button13_Click(object sender, System.EventArgs e) 

this.Datagrid4.Visible=true
Response.Clear(); 
Response.Buffer
= true
Response.Charset
="GB2312"
Response.AppendHeader(
"Content-Disposition","attachment;filename=File1.DOC"); 
Response.ContentEncoding
=System.Text.Encoding.GetEncoding("GB2312"); 
Response.ContentType 
= "application/ms-word"
this.Datagrid4.EnableViewState = false
System.IO.StringWriter oStringWriter 
= new System.IO.StringWriter(); 
System.Web.UI.HtmlTextWriter oHtmlTextWriter 
= new System.Web.UI.HtmlTextWriter (oStringWriter); 
this.Datagrid4.RenderControl(oHtmlTextWriter); 
Response.Write(oStringWriter.ToString()); 
Response.End(); 
}
 

在做ASP.NET项目时,会经常遇到要导出文件的问题,如将DataGrid中的数据导出到excel文件等,经常使用的是Office中的OWC组件,这个组件提供的功能很强大,在一般的项目中都可以满足当前的需要.但是这个功能强大的组件使用起来却不是很方便,不但有版本的问题,而且代码量也相对比较大.如果要利用Respone对象和相关的IO,也可以实现到处excel/word等文件,而且使用方便.

代码如下:

System.IO.StringWriter SW = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter HTW
=new System.Web.UI.HtmlTextWriter(SW);
Page.RenderControl(HTW);
//Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
Response.Buffer=true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType 
= "Response.ContentType";
//Response.ContentType是输出流的 HTTP MIME 类型
//Response.ContentType      --- word文件
//application/vnd.ms-excel --- excel文件
//
Response.Charset="utf-8";
Response.ContentEncoding
=System.Text.Encoding.GetEncoding("utf-8");
Response.AddHeader(
"Content-Disposition""attachment;filename=XXX.doc");
//attachment --- 作为附件下载
//inline --- 在线打开
//filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)
//进行进行编码,以解决文件名乱码的问题
Response.Write(SW.ToString());
Response.Flush();
Response.Close();

这样即可以将当前的页面导出成一个Word文件.同样原理要将DataGrid,DataList等中的内容导出为Word或Excel等类型的文件,那么只需要稍做修改代码即可.

   OK了,这样比用OWC组件方便多了,但也有个小问题,那就是导出的文件从文件格式上来说不是"真正"Word或Excel类型的文件,但用Office照样能打开,没什么区别,用户是看不出来的,也不会去理会这样的事情~~~