【问题标题】:Exporting GridView to XLS --- Paging Issue将 GridView 导出到 XLS --- 分页问题
【发布时间】:2012-01-18 10:11:10
【问题描述】:

我有一个 GridView,我打算将它导出到 .xls 文件中。在此网格视图中启用了分页。我目前使用的代码只能导出gridview的第一页。

   Response.Clear();
   Response.Buffer = true;

   Response.AddHeader("content-disposition","attachment;filename=DataTable.xls");
   Response.Charset = "";
   Response.ContentType = "application/vnd.ms-excel";

   StringWriter sw = new StringWriter();
   HtmlTextWriter hw = new HtmlTextWriter(sw);

   //As you notice, below I tried to disable the paging yet it's unsuccessful
   //FYI I am able to really prevent first column, header row, and footer row to
   //be exported through this

   gvGridView.Columns[0].Visible = false;
   gvGridView.HeaderRow.Visible = false;
   gvGridView.FooterRow.Visible = false;
   gvGridView.AllowPaging = false;

   for (int i = 0; i < gvGridView.Rows.Count; i++)
   {
       gvGridView.Rows[i].Attributes.Add("class", "textmode");
   }
   gvGridView.RenderControl(hw);

   string style = @"<style> .textmode { mso-number-format:\@; } </style>";
   Response.Write(style);
   Response.Output.Write(sw.ToString());
   Response.Flush();
   Response.End();

我注意到以前有人问过这个问题,但没有正确答案。

感谢任何帮助。

谢谢

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    你应该重新绑定你的GridView你指定gvGridView.AllowPaging = false;,然后导出。否则.RenderControl(hw); 将只呈现当前选择的GridView 页面。

    【讨论】:

    • 我注意到它还在工作。它以某种方式导出了我之前禁用的第一列、标题行和页脚行。
    • 我自己解决了这个问题。谢谢
    • 您是否在 RenderControl() 之前使它们不可见?你需要。
    【解决方案2】:

    更改 AllowPaging 属性后,只需创建一个 GridView.DataBind()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 2013-02-12
      相关资源
      最近更新 更多