c# - GridView导出Excel时如何隐藏到列?

标签 c#

GridView 导出到 Excel 时如何隐藏两列。

所以目前我导出时的样子是这样的:

-------------------------------------------------------------------------
|  Display Name  | Email Address    | License  |            |           |
-------------------------------------------------------------------------
| User 1         | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="47222a262e2b07222a262e2b6924282a" rel="noreferrer noopener nofollow">[email protected]</a>  |   1      |   edit     |  delete   |
-------------------------------------------------------------------------
| User 2         | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="70151d11191c4130151d11191c5e131f1d" rel="noreferrer noopener nofollow">[email protected]</a> |   1      |   edit     |  delete   |
-------------------------------------------------------------------------

如您所见,最后两列是我的 GridView 中用于编辑和删除行的链接 我不想将这两列导出到 Excel,因为它看起来不专业。

导出到 Excel 时是否可以删除(或隐藏)这两列?

这是我正在使用的代码:

protected void ExportToExcel(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=UserExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            //To Export all pages
            GridView1.AllowPaging = false;
            this.BindGrid();

            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();
        }
    }

最佳答案

这可能有用。它基本上应该将 gridview 复制到新的 gridview,然后删除最后两列,为其余代码做好准备。

protected void ExportToPDF(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=UserExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    using (StringWriter sw = new StringWriter())
    {
        HtmlTextWriter hw = new HtmlTextWriter(sw);


GridView gv = GridView1;
gv.Columns.RemoveAt(4);
gv.Columns.RemoveAt(3);

        //To Export all pages
        gv.AllowPaging = false;
        this.BindGrid();

        gv.HeaderRow.BackColor = Color.White;
        foreach (TableCell cell in gv.HeaderRow.Cells)
        {
            cell.BackColor = gv.HeaderStyle.BackColor;
        }
        foreach (GridViewRow row in gv.Rows)
        {
            row.BackColor = Color.White;
            foreach (TableCell cell in row.Cells)
            {
                if (row.RowIndex % 2 == 0)
                {
                    cell.BackColor = gv.AlternatingRowStyle.BackColor;
                }
                else
                {
                    cell.BackColor = gv.RowStyle.BackColor;
                }
                cell.CssClass = "textmode";
            }
        }

        gv.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();
    }
}

关于c# - GridView导出Excel时如何隐藏到列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59065193/

相关文章:

c# - 如何反转查询或c#中的单词顺序

c# - 将 linq 方法语法转换为查询语法

c# - Azure TimerTrigger 未触发

C# 和 VB.Net 输出参数

c# - 如何在c#中使用多个未选中的

c# - RichTextBox.Select 与 SubString 方法之间的行为不一致

c# - UWP 应用程序发布版本卡在启动屏幕上?

c# - 带有 MEF 的 ASP.NET MVC4 : Requests to MEF Controllers Processed Serially

c# - 在 C# winforms 中消除继承 "magic"的最佳方法?

c# - 如何在 C# 中等待 3 秒然后将 bool 设置为 true?