title: Issue Exporting Grid View Data To Excel
body: <p>I have to page in my web page that allows you to search for a list of names and it returns the results into a grid view. When the search result appears, the export button appears. I have the export button there for the user to click and it export the data from the grid view into excel in <code>xlsx</code> format. This is my current code:</p>
  
  <p><strong>ASP</strong></p>
  
  <pre><code>&lt;asp:TableCell&gt;
       &lt;asp:DataGrid ID="ResultGrid" runat="server"
        AutoGenerateColumns="false"
        EnableViewState="false" 
  &lt;/asp:TableCell&gt;
  &lt;asp:TableCell&gt;
       &lt;asp:Button class="myClass" ID="export" runat="server" OnClick="ExportBtn_Click" Text="Export" /&gt;
  &lt;/asp:TableCell&gt;
  </code></pre>
  
  <p><strong>C#</strong></p>
  
  <pre><code>protected void ExportBtn_Click(object sender, EventArgs e)
      {
          Response.Clear();
          Response.AddHeader("content-disposition", "attachment;filename=myexcelfile.xls");
  
          Response.ContentType = "application/vnd.xls";
  
          StringWriter sw = new StringWriter();
          HtmlTextWriter htw = new HtmlTextWriter(sw);
  
          ResultGrid.RenderControl(htw);
          Response.Write(sw.ToString());
          Response.End();
      }
  
      public override void VerifyRenderingInServerForm(Control control)
      {
  
      }
  </code></pre>
  
  <p>Currently this downloads a blank excel file... even though my grid view has one item in it upon search. </p>
  
  <p>My additional question (even though I know it is frowned upon), is how to export as <code>xlsx</code> instead of <code>xls</code>. If I replace the <code>xls</code> with <code>xlsx</code>, it tells me it is in the wrong format when I click my export button (when it tries to open up in excel).</p>
  
  <p>To wrap up, my two question are: why is it returning nothing in excel even though I have a row returned in grid view and how to export as <code>xlsx</code> instead of <code>xls</code>?</p>