vb.net 将 GridView 导出到 Excel 时删除编辑删除按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20046623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:52:24  来源:igfitidea点击:

Remove Edit Delete Button When Exporting GridView To Excel

asp.netvb.netgridviewexport-to-excel

提问by MohammadMMohammad

I Have GridView in ASP.Net application with AutoGenerateDeleteButton & AutoGenerateEditButton Set to True. I want when exporting gridview to excel to not show these button in excel sheet. My export code below:

我在 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

采纳答案by amitesh

After seraching i came up with this

搜索后我想出了这个

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 */
}

and on aspx page make change like

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

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

in vb i guess it look like this

在 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 is like this

.aspx页面是这样的

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

回答by MohammadMMohammad

The solution is to set them false when exporting:

解决方案是在导出时将它们设置为 false:

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