会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
HQT
追求.NET 技术永无止境
导航
博客园
首页
新随笔
联系
订阅
管理
公告
DataGrid 导出 EXCEL(简单,实用)
Posted on
2005-09-14 17:56
HQT
阅读(
3993
) 评论(
10
)
编辑
收藏
举报
Response.ContentEncoding
=
//
有时用 gb2312 不能正常显示中文,要用 utf-8
System.Text.Encoding.GetEncoding(
"
GB2312
"
);
Response.AppendHeader(
"
Content-Disposition
"
,
"
attachment;filename=
"
+
"
result.xls
"
);
//
必要,做成下载文件
//
如果要直接在浏览器中打开,把上行注释掉,换成下面这行
//
Response.ContentType = "application/vnd.ms-excel";
Response.Charset
=
""
;
//
从Content-Type header中去除charset设置
//
关闭 ViewState
this
.EnableViewState
=
false
;
System.IO.StringWriter tw
=
new
System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw
=
new
HtmlTextWriter(tw);
//
获取control的HTML
DataGrid1.RenderControl(hw);
//
把HTML写回浏览器
Response.Write(tw.ToString());
Response.End();
超精简,实用
.
如果DataGrid用了自动分页,那要在上面的程序之前加上:
DataGrid1.AllowPaging
=
false
;
BindDataGrid();
//
重新绑定.
..
//
上面的输出代码
..
DataGrid1.AllowPaging
=
true
;
会员力量,点亮园子希望
刷新页面
返回顶部
Powered by:
博客园
Copyright © 2024 HQT
Powered by .NET 8.0 on Kubernetes