title: How to Export multiple header Gridview Data to Excel
body: <p>I have a gridview which has multiple headers added during run time as shown in snap below:
  <a href="https://i.stack.imgur.com/vEV4M.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vEV4M.png" alt="enter image description here" /></a></p>
  <p>I want to export the data from this gridview to excel for which I have the following code:</p>
  <pre><code>protected void Button1_Click(object sender, EventArgs e)
      {
          Response.Clear();
          Response.Buffer = true;
          Response.AddHeader(&quot;content-disposition&quot;, &quot;attachment;filename=GridViewExport.xls&quot;);
          Response.Charset = &quot;&quot;;
          Response.ContentType = &quot;application/vnd.ms-excel&quot;;
          using (StringWriter sw = new StringWriter())
          {
              HtmlTextWriter hw = new HtmlTextWriter(sw);
              gdView.HeaderRow.BackColor = Color.White;
              foreach (TableCell hcell in gdView.HeaderRow.Cells)
              {
                  hcell.BackColor = Color.White;
              }
              foreach (GridViewRow row in gdView.Rows)
              {
                  {
                      row.BackColor = Color.White;
                      foreach (TableCell cell in row.Cells)
                      {
                          cell.CssClass = &quot;textmode&quot;;
                      }
                  }
              }
              gdView.RenderControl(hw);
              //style to format numbers to string
              string style = @&quot;&lt;style&gt; .textmode { } &lt;/style&gt;&quot;;
              Response.Write(style);
              Response.Output.Write(sw.ToString());
              Response.Flush();
              Response.End();
          }
      }
      public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
      {
          // controller   
      }
  </code></pre>
  <p>The excel output I am getting is as follows:</p>
  <p><a href="https://i.stack.imgur.com/dxKto.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dxKto.png" alt="enter image description here" /></a></p>
  <p>As you can see the color of the header rows is continuing beyond the table area. Also the top header rows are collapsed in the excel output.</p>
  <p>Request you all to kindly suggest how this can be resolved.</p>
  <p>Thanks.</p>