0

我有一个 GridView 并且在后面的代码中,我将其转换为页面中按钮单击事件上的 Excel 工作表............代码在 aspx 中如下所示。

<form id="form1" runat="server">
    <table width="1100" border="0" cellpadding="0" cellspacing="0" style="height:100%; background-color:White" align="center">
            <tr><td>&nbsp;</td></tr>
            <tr>
                <td align="center">
                    <asp:Button ID="DividendEportToExcel" runat="server" Text="Convert To Excel" 
                        CssClass="bluesome" onclick="DividendEportToExcel_Click" />      
                </td>
            </tr>
            <tr>
                <td>
                     <asp:GridView ID="MemberDividendView" runat="server">
                               </asp:GridView>
               </td>
           </tr>
     </table>
</form>

在后面的代码中,我正在执行以下操作:

protected void DividendEportToExcel_Click(object sender, EventArgs e)
    {
        MemberDividendView.Columns.RemoveAt(5);
        MemberDividendView.ShowHeader = true;
        MemberDividendView.GridLines = GridLines.Both;
        MemberDividendView.PagerSettings.Visible = false;
        MemberDividendView.DataBind();
        ChangeControlsToValue(MemberDividendView);
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=AdjustmentDetialsToExcel.xls");
        Response.ContentType = "application/excel";
        StringWriter sWriter = new StringWriter();
        HtmlTextWriter hTextWriter = new HtmlTextWriter(sWriter);
        HtmlForm hForm = new HtmlForm();
        MemberDividendView.Parent.Controls.Add(hForm);
        hForm.Attributes["runat"] = "server";
        hForm.Controls.Add(MemberDividendView);
        hForm.RenderControl(hTextWriter);
        Response.Write(sWriter.ToString());
        Response.End();
      }

    private void ChangeControlsToValue(Control gridView)
    {
        Literal literal = new Literal();

        for (int i = 0; i < gridView.Controls.Count; i++)
        {
            if (gridView.Controls[i].GetType() == typeof(LinkButton))
            {

                literal.Text = (gridView.Controls[i] as LinkButton).Text;
                gridView.Controls.Remove(gridView.Controls[i]);
                //gridView.Controls.AddAt(i,literal);
            }
            else if (gridView.Controls[i].GetType() == typeof(DropDownList))
            {
                literal.Text = (gridView.Controls[i] as DropDownList).SelectedItem.Text;
                gridView.Controls.Remove(gridView.Controls[i]);
                //gridView.Controls.AddAt(i,literal);
            }
            else if (gridView.Controls[i].GetType() == typeof(CheckBox))
            {
                literal.Text = (gridView.Controls[i] as CheckBox).Checked ? "True" : "False";
                gridView.Controls.Remove(gridView.Controls[i]);
                //gridView.Controls.AddAt(i,literal);
            }
            if (gridView.Controls[i].HasControls())
            {
                ChangeControlsToValue(gridView.Controls[i]);

            }
        }
    }

但是我的列仍然没有在 Excel 表中被删除........错误是什么?

4

1 回答 1

1
  1. GridView如果要在导出前删除列,则需要将 DataBind 绑定到它的 DataSource。
  2. 尝试设置它们Viusible=false而不是删除它们
  3. 您不需要创建新的服务器表单来避免服务器控件在表单之外呈现的 ASP.NET 异常。

    您可以通过覆盖VerifyRenderingInServerForm来避免这种情况

    public override void VerifyRenderingInServerForm(Control control)
    {
      /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
         server control at run time. */
    }
    
  4. 我建议创建一个真正的 excel 文件,而不是仅从 excel 解释的 HTML 表。我可以推荐EPPlus

于 2012-04-23T10:21:16.403 回答