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

推荐订阅源

WordPress大学
WordPress大学
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
月光博客
月光博客
V
Visual Studio Blog
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
Recorded Future
Recorded Future
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
F
Full Disclosure
The Cloudflare Blog
Y
Y Combinator Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
AI
AI
N
News and Events Feed by Topic
T
Tor Project blog
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog

博客园 - xiazhaoxia

关于《会员专区》项目经验分享 dell的1501和640m,买哪个好呢? 数码相机成像好坏的关键?! 我要试试洋酒! 我爱网购! You are my everything Remeber me by 李孝利 关于oracle中的去除null和空格的问题 致力于blog的新衣服 datagrid小tip(三):导入Excel的问题?! datagrid小tip(二):拼接多层表头 datagrid小tip(一):鼠标的移动和点击变色 相对路径or绝对路径? DataGrid的动态绑定问题(二) 续 oracle中的联合主键查询问题! DataGrid的动态绑定问题(二) __doPostBack()无效 ? 屏蔽页面刷新功能 asp.net学习点滴
DataGrid数据导出到Excel
xiazhaoxia · 2007-03-21 · via 博客园 - xiazhaoxia

网上最常见的导出代码,这个方法的优点是简单.但是缺点就是不能格式化.
如果datagrid中有linkbutton列,导出程序就会报错
类型“DataGridLinkButton”的控件“DataGrid1__ctl2__ctl0”必须放在具有 runat=server 的窗体标记内。纠正这个错误的方法就是对datagrid进行重新绑定,当然在重新绑定之前最好设置参数,这样查询的时候是LinkButton列,导出的时候是普通列或不生成该列.
我实现的方法,修改导出函数如下:

Session["print"]="1";


SetBind();
//重新绑定datagrid
Session.Remove("print");//注意:清理session的部分不能写在DataGrid1_ItemDataBound中
                        
//因为DataGrid1_ItemDataBound是一行一行绑的.如果写在里面就
                        
//表示绑定了第一行,等第二行的时候还是会绑定LinkButton.

Response.Clear(); 
Response.Buffer
= true
Response.Charset
="GB2312";    
Response.AppendHeader(
"Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentEncoding
=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
this.DataGrid1.Page.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);
this.DataGrid1.RenderControl(oHtmlTextWriter); 
Response.Write(oStringWriter.ToString());
Response.End();


 1 private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
 2         {    
 3             if(Session["print"!=null)
 4             {
 5                 if(Session["print"].ToString()=="1")
 6                 {
 7                     e.Item.Cells[0].Visible =false;
 8                     e.Item.Cells[1].Visible =false;
 9                     e.Item.Cells[2].Visible =false;                    
10                     e.Item.Cells[12].Visible  =false;                    
11                 }        
12             }
13         }

posted on 2007-03-21 15:48  xiazhaoxia  阅读(587)  评论()    收藏  举报