0

我正在解决一个带有大量复选框的 Gridview 问题,该问题带有导出到 Excel 的复选框(下面的示例代码。实际代码包括更多复选框)。发生的情况是 1) 用户单击按钮开始导出过程,然后 2) 用户将文件保存或下载到他们的桌面,3) 文件需要相当长的时间来渲染基本单元格 (> 15秒),然后一旦基本的 excel 文件呈现 3)电子表格呈现复选框(呈现时间需要 3 到 10 分钟,具体取决于计算机的速度)并且似乎挂起,直到复选框出现在 GridView 中。

我还应该注意,这个示例文件大约是 800k,我几乎可以立即将其他较大的网格视图导出到 Excel,而无需复选框。

我知道从网页实际导出的文件不是真正的 Excel 文件,直到您将其保存为电子表格/工作簿。但是,带有复选框的网格视图需要很长时间才能呈现为“电子表格”,这似乎有点奇怪。

我的问题是 - 这是导出带有复选框的 Excel 电子表格的正常行为,还是有什么方法可以改进下面的代码以确保 Excel 电子表格在打开时不会长时间挂起?有没有其他解决方案可以帮助加快复选框渲染时间?

aspx 代码

<%@ Page Title="Test Page" Language="C#" MasterPageFile="~/Site.master" EnableEventValidation="false" AutoEventWireup="true" CodeFile="form2.aspx.cs" Inherits="form2" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server"></asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" Runat="Server"></asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="MainContent" Runat="Server">
  <asp:Button ID="Button1" runat="server" Height="38px"  OnClick="Button1_CLick" Text="Export to Excel" Width="200px" />
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="Field 1" HeaderText="Field 1" SortExpression="Field 1" />
        <asp:BoundField DataField="Field 2" HeaderText="Field 2" SortExpression="Field 2" />
        <asp:BoundField DataField="Field 3" HeaderText="Field 3" SortExpression="Field 3" />
        <asp:BoundField DataField="Field 4" HeaderText="Field 4" SortExpression="Field 4" />
        <asp:BoundField DataField="Field 5" HeaderText="Field 5" SortExpression="Field 5" />
        <asp:BoundField DataField="Field 6" HeaderText="Field 6" SortExpression="Field 6" />
        <asp:BoundField DataField="Field 7" HeaderText="Field 7" SortExpression="Field 7" />
        <asp:BoundField DataField="Field 8" HeaderText="Field 8" SortExpression="Field 8" />
        <asp:BoundField DataField="Field 9" HeaderText="Field 9" SortExpression="Field 9" />
      <asp:Panel ID="pnlClick" runat="server" CssClass="collapsiblepanelCSS">
          <div style="background-color:blue;height:30px; vertical-align: middle">
              <div style="float: left;color:white;padding: 5px 5px 0 0 ">
                  Server List Collapsable Panel
              </div>
              <div style="float: right; color: white; padding: 5px 5px 0 0" id="divshow">
                  <asp:Label ID="lblMessage" runat="server" Text="Label" />
                  </div>
              <div style="clear:both">
            </div>
                </div>
              </asp:Panel>
        <asp:Panel ID="pnlCollapsable" runat="server" Height="0" CssClass="collapsiblepanelCSS">
        <asp:CheckBoxField DataField="Check Box 1" HeaderText="Check Box 1" SortExpression="Check Box 1" />
        <asp:CheckBoxField DataField="Check Box 2" HeaderText="Check Box 2" SortExpression="Check Box 2" />
        <asp:CheckBoxField DataField="Check Box 3" HeaderText="Check Box 3" SortExpression="Check Box 3" />
        <asp:CheckBoxField DataField="Check Box 4" HeaderText="Check Box 4" SortExpression="Check Box 4" />
        <asp:CheckBoxField DataField="Check Box 5" HeaderText="Check Box 5" SortExpression="Check Box 5" />
        <asp:CheckBoxField DataField="Check Box 6" HeaderText="Check Box 6" SortExpression="Check Box 6" />
        <asp:CheckBoxField DataField="Check Box 7" HeaderText="Check Box 7" SortExpression="Check Box 7" />
        <asp:CheckBoxField DataField="Check Box 8" HeaderText="Check Box 8" SortExpression="Check Box 8" />
        <asp:CheckBoxField DataField="Check Box 9" HeaderText="Check Box 9" SortExpression="Check Box 9" />
        <asp:CheckBoxField DataField="Check Box 10" HeaderText="Check Box 10" SortExpression="Check Box 10" />
        <asp:CheckBoxField DataField="Check Box 11" HeaderText="Check Box 11" SortExpression="Check Box 11" />
        <asp:CheckBoxField DataField="Check Box 12" HeaderText="Check Box 12" SortExpression="Check Box 12" />
          </asp:Panel>
        <CC1:CollapsiblePanelExtender ID="CollapsiblePanelExtender1" Collapsed="true" ExpandControlID="pnlClick" TextLabelID="lblMessage" CollapsedText="Click To Show" ExpandedText="Click To Hide" ImageControlID="imgArrows" ExpandDirection="Vertical" TargetControlID="pnlCollapsable" ScrollContents="false" runat="server"></CC1:CollapsiblePanelExtender>

        <asp:BoundField DataField="Field 10" HeaderText="Field 10" SortExpression="Field 10" />
        <asp:BoundField DataField="Field 11" HeaderText="Field 11" SortExpression="Field 11" />
        <asp:BoundField DataField="Field 12" HeaderText="Field 12" SortExpression="Field 12" />
        <asp:CommandField ShowEditButton="True" />
    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>

<sql connection strings, insert/update SQL code, update parameters, and closing ASP/HTML tags>

.cs 文件

   using System;
   using System.Configuration;
   using System.Data;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.UI;
   using System.Web.UI.WebControls;

   public partial class form2 : system.Web.UI.Page
   {

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override void VerifyRenderingInServerForm(Control control)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
              Response.Clear();
              Response.AddHeader("content-disposition", "attachement;filename=FileNAme.xls");
              Response.ContentType = "application/vnd.xlsx";
              System.IO.StringWriter stringWrite = new System.IO.StringWriter();
              System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
              GridView1.RenderControl(htmlWrite);
              Response.Write(stringWrite.ToString());
              Response.End();
        }


    } 
4

1 回答 1

0

经过一些研究后,我确定从 gridview 导出到 Excel 的大量复选框(尤其是当 gridview 相当大时)需要相当长的导出过程才能正确呈现所有复选框到最终产品。

当然,导出的 Excel 文档在专门保存为电子表格之前不是真正的电子表格。我只是想“记录”一下,复选框渲染过程在导出时需要相当长的时间。也许这可以使这个过程在标签中注明的产品的未来版本中不那么冗长。

于 2013-10-07T16:06:47.160 回答