Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在excel中导出网格视图时如何跳过某些列?_Asp.net_C# 4.0_Gridview - Fatal编程技术网

Asp.net 在excel中导出网格视图时如何跳过某些列?

Asp.net 在excel中导出网格视图时如何跳过某些列?,asp.net,c#-4.0,gridview,Asp.net,C# 4.0,Gridview,我有一个网格视图。其中包含一些列。假设它有10列,其中只有8列有标题,而2列标题为空。现在我将这个gridview导出到excel中,它包含所有10列,其中2列没有名称如何在将GridView导出到excel时排除这两个空标题列。我使用以下代码进行相同的操作: protected void btnExportToExcel_Click(object sender, EventArgs e) { Export("Customers.xls",

我有一个网格视图。其中包含一些列。假设它有10列,其中只有8列有标题,而2列标题为空。现在我将这个gridview导出到excel中,它包含所有10列,其中2列没有名称如何在将GridView导出到excel时排除这两个空标题列。我使用以下代码进行相同的操作:

        protected void btnExportToExcel_Click(object sender, EventArgs e)
        {
            Export("Customers.xls", this.gdvMessages);
        }

        private void Export(string fileName, GridView gv)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
            HttpContext.Current.Response.ContentType = "application/ms-excel";

            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    //  Create a form to contain the grid
                    Table table = new Table();

                    //  add the header row to the table
                    if (gv.HeaderRow != null)
                    {
                        PrepareControlForExport(gv.HeaderRow);
                        table.Rows.Add(gv.HeaderRow);
                    }

                    //  add each of the data rows to the table
                    foreach (GridViewRow row in gv.Rows)
                    {
                        PrepareControlForExport(row);
                        table.Rows.Add(row);
                    }

                    //  add the footer row to the table
                    if (gv.FooterRow != null)
                    {
                        PrepareControlForExport(gv.FooterRow);
                        table.Rows.Add(gv.FooterRow);
                    }

                    //  render the table into the htmlwriter
                    table.RenderControl(htw);

                    //  render the htmlwriter into the response
                    HttpContext.Current.Response.Write(sw.ToString());
                    HttpContext.Current.Response.End();
                }
            }
        }

如果您不需要这两列,就不能将它们包含在HTML表中。。。。或者,当用户按下“导出到Excel”按钮时,您可以重新写入表格,而不包括列。您还可以让该表加上另一个具有隐藏列的表,并在用户单击“导出到Excel”按钮时从中获取数据。此外,您根本无法显示数据,当用户加载页面或单击按钮时,将下载没有列的数据。祝你好运

已编辑:对于此独占方案,您可能希望按以下方式编辑代码:

 //  add the header row to the table
            if (gv.HeaderRow != null)
            {
                TableRow tr = new TableRow();

                foreach (DataControlFieldHeaderCell c in gv.HeaderRow.Cells)
                {
                    if (c.Text != " ") {
                        tr.Cells.Add(new TableCell() { Text = c.Text});
                    }
                }

                PrepareControlForExport(tr);
                table.Rows.Add(tr);
            }
这部分代码的作用是循环遍历标题行中的每个单元格,如果它为空,则不包括它。然后将其添加到html表中。如果需要,可以对其进行调整,以便表的其余部分在这些列所属的位置添加一个cellspan。如果这不是你所需要的,也许你的实际Gridview和所需的输出屏幕截图夫妇将是理想的


祝你好运

相反,您可以使用以下代码在导出到excel时隐藏这些列

this.myGridview.Columns[0].Visible=false


其中“0”是我们要隐藏的列的索引。

您能给我举个“不要在HTML表中包含这两列”的例子吗?我和Sandy有同样的需要,只是我使用的是使用旧vb.net代码的DataGrid控件。user2508258的想法为我解决了这个问题。我的vb.net代码:DataGrid1.Columns(0).Visible=False(我想在导出到Excel的过程中删除第一列)。