How to skip some column when exporting Grid view to excel?

I have a grid. Which contains several columns. Suppose it has 10 columns, of which only 8 columns have a heading, and 2 column headings are empty. Now I export this gridview to excel, which contains all 10 columns with 2 columns without a name. How can I exclude 2 empty header columns when exporting a GridView to excel . For this, I use the following code:

        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();
                }
            }
        }
+5
source share
2 answers

HTML, .... , " Excel" , , " Excel". , , , "" . !

EDITED: :

 //  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. , , , . , , , Gridview .

!

+3

excel,

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

'0' - , .

+2

All Articles