killkill
专注于技术
博客园
首页
博问
闪存
新随笔
联系
订阅
管理
随笔-238 文章-2 评论-619
关于Asp.net中使用以下代码导出Excel表格的问题
private
void
ExportExcelFromDataGrid(
string
filename , System.Web.UI.WebControls.DataGrid ToExcelGrid )
{
Response.Clear();
Response.Buffer
=
true
;
Response.Charset
=
"
utf-8
"
;
Response.AppendHeader(
"
Content-Disposition
"
,
"
attachment;filename=
"
+
Server.UrlEncode ( filename ) );
Response.ContentEncoding
=
System.Text.Encoding.Default;
//
设置输出流为简体中文
Response.ContentType
=
"
application/ms-excel
"
;
//
设置输出文件类型为excel文件。
this
.EnableViewState
=
false
;
System.Globalization.CultureInfo myCItrad
=
new
System.Globalization.CultureInfo(
"
ZH-CN
"
,
true
);
System.IO.StringWriter oStringWriter
=
new
System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter
=
new
System.Web.UI.HtmlTextWriter(oStringWriter);
ToExcelGrid.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
private
void
Button4_Click(
object
sender, System.EventArgs e)
{
this
.Panel1.Visible
=
false
;
string
filename
=
"
内训师.xls
"
;
this
.DataGrid1.Columns[
6
].Visible
=
true
;
this
.DataGrid1.Columns[
7
].Visible
=
true
;
this
.DataGrid1.Columns[
this
.DataGrid1.Columns.Count
-
1
].Visible
=
false
;
this
.DataGrid1.Columns[
this
.DataGrid1.Columns.Count
-
2
].Visible
=
false
;
this
.DataGrid1.AllowSorting
=
false
;
this
.DataGrid1.AllowPaging
=
false
;
this
.DataGrid1.SelectedIndex
=
-
1
;
this
.BindGrid();
this
.ExportExcelFromDataGrid ( filename ,
this
.DataGrid1 );
}
原理是利用DataGrid(其实是其父类Control)的RenderControl方法输出,整个DataGrid的外观时,将这些HTML代码写入到缓冲区,同时设置一下 ContentType ,让Excel自己的自动纠错功能将这个输出存为一个Excel文件。
很多网上的朋友使用了以上这种可粘贴性强的代码,发现不好用,反映的错误类似:
“LinkButton必须放在一个具有runat=server的标签的Form”之类的话,而更多的网友说:
问题很明显,因为DataGrid没有放在runat=server 的Form里面,加上就可以了。
我认为,这种回答是很不准确的,理由如下:
通常使用这种代码的人他/她的DataGrid,最起码已经能用了,所以必定放在那个具有runat=server 的form标签里了。
报错是LinkButton,而不是DataGrid,很多细心的朋友很可能会说,我一直在用DataGrid,LinkButton在哪里来的。
其实真正的问题是,上面的代码没有加了 // * 的那几行代码引起的。
当然如果你的DataGrid,没有排序,没有使用那种按钮列的话,是不会出问题的。
言归正传,LinkButtion其实是你将DataGrid设为可排序时候,的表头包含的,所以我要将DataGrid的排序设为False。
同样得到上面的其实,那些按钮列,什么"编辑"、“删除”等等这些,也是用了LinkButton,把他设为不可见就是了。
其实说得再明白一点就是,将有可能产生LinkButton,或其他控件的东西都不让它输出就是了
绿色通道:
好文要顶
关注我
收藏该文
与我联系
posted @ 2006-09-21 08:37
killkill
阅读(1090)
评论(0)
编辑
收藏
注册用户登录后才能发表评论,请
登录
或
注册
,
返回博客园首页
。
首页
博问
闪存
新闻
园子
招聘
知识库
最新IT新闻
:
·
伊朗封杀Gmail和Facebook等互联网服务
·
分析称专利之争让谷歌苹果两败俱伤
·
Android平台发现新型手机病毒Rootsmart
·
HTC首款Android4.0手机大曝光
·
这是不是你期待的 iPad 3?
»
更多新闻...
最新知识库文章
:
·
高级编程语言的发展历程
·
如何学习一门新的编程语言?
·
学习不同编程语言的重要性
·
为什么我喜欢富于表达性的编程语言
·
计算机专业的女生为什么要学编程
»
更多知识库文章...
China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
公告
昵称:
killkill
园龄:
5年4个月
荣誉:
推荐博客
粉丝:
67
关注:
20
<
2006年9月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
搜索
常用链接
我的随笔
我的评论
我的参与
最新评论
我的标签
最新随笔
1. MySQL重复记录删除
2. 使用Screen抵御杯具
3. [摘]Oracle 11g Flashback_transaction_query的undo_sql为空?
4. Duplicate Active Database 遇到 ORA-01017 invalid username/password
5. [摘]如何抓住蝴蝶效应中的那只蝴蝶
6. [原]将Oracle 中的blob导出到文件中
7. [原]unique index和non unique index的区别
8. [摘]Oracle限制某个数据库帐号只能在特定机器上连入数据库
9. [摘]将“相关值”导致的Nested Loop优化成Hash Join
10. [原]试用google的“手气不错”
我的标签
Oracle
(82)
Sql Server 2005
(24)
linux
(24)
performance tunning
(15)
hardware
(12)
Solaris
(12)
SQL
(10)
SQLServer2000
(8)
sqlplus
(5)
ESX
(5)
更多
随笔分类
(265)
C/C++(2)
(rss)
DB2(1)
(rss)
Fall in OCP(19)
(rss)
Linux(25)
(rss)
MirrorBuilder
(rss)
MySQL
(rss)
nginx(1)
(rss)
Oracle(74)
(rss)
ORA-Errors(8)
(rss)
Perl(3)
(rss)
Socket(1)
(rss)
Solaris(10)
(rss)
SQL Server 极高可用性架构(5)
(rss)
SQL Server 实践(40)
(rss)
SQL之美(10)
(rss)
Study .net EveryTime(11)
(rss)
TSM(2)
(rss)
Working@Alibaba
(rss)
Working@NC(51)
(rss)
存储“山寨”化
(rss)
乒乓乒乓(2)
(rss)
随笔档案
(239)
2011年12月 (1)
2011年9月 (1)
2011年6月 (1)
2011年5月 (2)
2011年4月 (4)
2011年3月 (2)
2011年2月 (5)
2011年1月 (5)
2010年12月 (5)
2010年11月 (6)
2010年10月 (6)
2010年9月 (14)
2010年8月 (11)
2010年7月 (16)
2010年6月 (19)
2010年5月 (9)
2010年4月 (9)
2010年3月 (3)
2010年2月 (3)
2010年1月 (5)
2009年12月 (12)
2009年11月 (2)
2009年10月 (11)
2009年9月 (2)
2009年8月 (8)
2009年7月 (6)
2009年6月 (3)
2009年5月 (6)
2009年4月 (11)
2009年3月 (10)
2009年2月 (11)
2009年1月 (10)
2008年12月 (5)
2008年11月 (3)
2008年10月 (4)
2008年9月 (2)
2008年6月 (1)
2008年5月 (1)
2006年9月 (4)
相册
两只乌龟
常去的地方
AnySQL
bbs.watchstor.com
PowerShell 脚本中心
PowerShell.com
www.commandlinefu.com
异常NB的shell命令大集合
土人
一位IT老前辈
大牛场
BA7NQ
blue_prince
(rss)
Eagle Fan
(rss)
Fenng 冯大辉
hello dba
http://www.dbainfo.net/
IBM DB2学习日记
Inside the Oracle Optimizer
kaya
Lucas Jellema
(rss)
newkid
Oracle Question and Answer
piner陈吉平
the third dba
TimesTen
(rss)
白鳝
熊军
朋友的地盘
Hold's Blog
pangwa's Blog
robaggio's Blog
周大师的OracleWorld
积分与排名
积分 - 208262
排名 - 405
最新评论
阅读排行榜
评论排行榜
推荐排行榜