title: How to skip some column while exporting Grid view in excel?
body: <p>I have a grid view. Which contains some columns. Suppose, it has 10 columns out of them only 8 columns has header while 2 <strong>columns header are empty.</strong> Now i am exporting this gridview into excel which contains all 10 columns with 2 columns without name. <strong>How can i exclude that 2 empty header columns while exporting GridView into excel</strong>. I am using following code for the same:</p>
  
  <pre><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();
                  }
              }
          }
  </code></pre>