GridView导出Excel 在线等,很急

风2013 2010-12-08 02:50:57
GridView 导出excel 为甚么在2003中打开没有问题,在2007中打开包格式错误
public static void Export ( GridView gv, bool b ) {
HttpContext.Current.Response.Clear ();
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
HttpContext.Current.Response.AddHeader (
"content-disposition", "attachment; filename=ss.xls" );
HttpContext.Current.Response.ContentType = "application/vnd.xls";
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");

using ( StringWriter sw = new StringWriter () ) {
using ( HtmlTextWriter htw = new HtmlTextWriter ( sw ) ) {
// Create a form to contain the grid 创建一个表格,包含网格
Table table = new Table ();
if ( b ) {
table.GridLines = gv.GridLines;
}
// add the header row to the table 标题行添加到表
if ( gv.HeaderRow != null ) {
GridViewExportUtil.PrepareControlForExport ( gv.HeaderRow );
table.Rows.Add ( gv.HeaderRow );
}

// add each of the data rows to the table 添加每一行数据到表
foreach ( GridViewRow row in gv.Rows ) {
GridViewExportUtil.PrepareControlForExport ( row );
table.Rows.Add ( row );
}

// add the footer row to the table 添加页脚行到表
if ( gv.FooterRow != null ) {
GridViewExportUtil.PrepareControlForExport ( gv.FooterRow );
table.Rows.Add ( gv.FooterRow );
}

try {
//屏蔽第一列
for ( int i = 0; i < table.Rows.Count; i++ ) {
table.Rows[ i ].Cells[ 10 ].Visible = false;
table.Rows[ i ].Cells[ 9 ].Visible = false;
//table.Rows[ i ].Cells[ 9 ].Visible = false;
table.Rows[ i ].Cells[ 6 ].Visible = false;
table.Rows[ i ].Cells[ 0 ].Visible = false;
}
}
catch ( Exception ) {
;
}

// render the table into the htmlwriter 渲染到htmlwriter表
table.RenderControl ( htw );

// render the htmlwriter into the response 渲染到响应的htmlwriter
HttpContext.Current.Response.Write ( sw.ToString () );
HttpContext.Current.Response.End ();
}

这是我的代码

请高手指教
...全文
104 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lonenan 2010-12-08
  • 打赏
  • 举报
回复
膜拜孟老大
ITcql 2010-12-08
  • 打赏
  • 举报
回复
孟老大的文章就算不错的说
孟子E章 2010-12-08
  • 打赏
  • 举报
回复
给你一个不报告格式错误的方法,拷贝即可测试运行,兼容2003,2007,2010

http://dotnet.aspx.cc/file/Export-Gridview-To-Excel-With-Multi-Sheet.aspx

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧