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

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
The Last Watchdog
The Last Watchdog
Cyberwarzone
Cyberwarzone
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
The Cloudflare Blog
V
V2EX
博客园_首页
博客园 - 聂微东
Vercel News
Vercel News
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
T
Tenable Blog
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
SecWiki News
SecWiki News
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
博客园 - 【当耐特】
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
A
About on SuperTechFans
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
D
DataBreaches.Net
P
Privacy & Cybersecurity Law Blog
Schneier on Security
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
D
Docker
P
Proofpoint News Feed

博客园 - 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照样能打开,没什么区别,用户是看不出来的,也不会去理会这样的事情~~~