【问题标题】:Remove Edit Delete Button When Exporting GridView To Excel将 GridView 导出到 Excel 时删除编辑删除按钮
【发布时间】:2013-11-18 11:28:02
【问题描述】:

我在 ASP.Net 应用程序中有 GridView,AutoGenerateDeleteButton 和 AutoGenerateEditButton 设置为 True。我希望在将 gridview 导出到 excel 时不在 excel 表中显示这些按钮。我的导出代码如下:

    Private Sub ExportGridView()
    Dim attachment As String = "attachment; filename=FileName.xls"
    Response.ClearContent()
    Response.AddHeader("content-disposition", attachment)
    Response.ContentType = "application/ms-excel"
    Dim sw As New IO.StringWriter
    Dim frm As HtmlForm = New HtmlForm()
    Page.EnableViewState = False
    Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
    frm.Attributes("runat") = "server"
    Controls.Add(frm)

    gvResults.AllowPaging = False
    gvResults.AllowSorting = False
    BindGrid()

    gvResults.DataBind()
    gvResults.Columns(14).Visible = False
    gvResults.Columns(15).Visible = False
    gvResults.Columns(16).Visible = False
    frm.Controls.Add(gvResults)
    frm.RenderControl(htw)
    Response.Write(sw.ToString())
    Response.End()
End Sub

【问题讨论】:

    标签: asp.net vb.net gridview export-to-excel


    【解决方案1】:

    搜索后我想出了这个

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=User.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            GridView1.AllowPaging = false;
            GridView1.AllowSorting = false;
            GridView1.Columns[14].Visible = false;
            GridView1.Columns[15].Visible = false;
            GridView1.Columns[16].Visible = false;
            BindGird();
            GridView1.HeaderRow.BackColor = Color.White;
            foreach (TableCell cell in GridView1.HeaderRow.Cells)
            {
                cell.BackColor = GridView1.HeaderStyle.BackColor;
            }
            foreach (GridViewRow row in GridView1.Rows)
            {
                row.BackColor = Color.White;
                foreach (TableCell cell in row.Cells)
                {
                    if (row.RowIndex % 2 == 0)
                    {
                        cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                    }
                    else
                    {
                        cell.BackColor = GridView1.RowStyle.BackColor;
                    }
                    cell.CssClass = "textmode";
                }
            }
    
            GridView1.RenderControl(hw);
    
            //style to format numbers to string
            string style = @"<style> .textmode { } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
    
            GridView1.Dispose();
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
    

    并在 aspx 页面上进行更改,如

    <%@ Page Title="" Language="C#" EnableEventValidation="false"%>
    

    在vb中我猜它看起来像这样

     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Response.Clear
        Response.Buffer = true
        Response.AddHeader("content-disposition", "attachment;filename=User.xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-excel"
        Dim sw As StringWriter = New StringWriter
        Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
        GridView1.AllowPaging = false
        GridView1.AllowSorting = false
        GridView1.Columns(14).Visible = false
        GridView1.Columns(15).Visible = false
        GridView1.Columns(16).Visible = false
        BindGird
        GridView1.HeaderRow.BackColor = Color.White
        For Each cell As TableCell In GridView1.HeaderRow.Cells
            cell.BackColor = GridView1.HeaderStyle.BackColor
        Next
        For Each row As GridViewRow In GridView1.Rows
            row.BackColor = Color.White
            For Each cell As TableCell In row.Cells
                If ((row.RowIndex Mod 2)  _
                            = 0) Then
                    cell.BackColor = GridView1.AlternatingRowStyle.BackColor
                Else
                    cell.BackColor = GridView1.RowStyle.BackColor
                End If
                cell.CssClass = "textmode"
            Next
        Next
        GridView1.RenderControl(hw)
        'style to format numbers to string
        Dim style As String = "<style> .textmode { } </style>"
        Response.Write(style)
        Response.Output.Write(sw.ToString)
        Response.Flush
        Response.End
        GridView1.Dispose
    End Sub
    
    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
    
    End Sub
    

    .aspx页面是这样的

    <%@ Page Title="" Language="C#" EnableEventValidation="false"%>
    

    【讨论】:

    • 这不起作用,因为它们不是第一列,它们是在列中构建的,我首先尝试过,但没有用
    • 尝试显示 gvreseult.Columns(14).Visible=true;在 Response.End() 之后和其他列相同
    • 14,15,16 是不同的列,我也不想导出它们。我的问题是我不想在 Excel 中显示编辑和删除链接,它们在启用 AutoGenerateDeleteButton 和 AutoGenerateEditButton 后出现在 gridview 中
    • 这个解决方案在我的机器上运行
    【解决方案2】:

    解决办法是在导出的时候设置为false:

        gvResults.AllowPaging = False
        gvResults.AllowSorting = False
        gvResults.AutoGenerateEditButton = False
        gvResults.AutoGenerateDeleteButton = False
        BindGrid()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      相关资源
      最近更新 更多