title: Export to Excel
body: <p>I would like to export a GridView to excel, which is easy enough. But above the grid, in Excel, I would like some other information for identification. Can I somehow export things other than gridviews while then putting in the gridview below?</p>
  
  <p><strong>Edit:</strong>
  For some reason when the GridView1 is visible and I try to export, the entire page exports and not just the gridview. Not sure why!</p>
  
  <pre><code>Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
      'Create a StringWriter and HtmlTextWriter
      Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
      Dim htw As New System.Web.UI.HtmlTextWriter(sw)
  
      'Clear the Response object's content and specify the header for the HTML response and type of application file to create
      Response.ClearContent()
      Response.AddHeader("content-disposition", "attachment; filename=SaveFile.xls")
      Response.ContentType = "application/vnd.ms-excel"
      Response.Charset = ""
      EnableViewState = False
  
      htw.WriteLine("Test, test, test")
  
      Try
          'Check for the number of GridView rows
          If GridView1.Rows.Count &lt; 65535 Then
              'Turn sorting and paging off and rebind the GridView control
              GridView1.AllowSorting = False
              GridView1.AllowPaging = False
              GridView1.PageSize = GridView1.Rows.Count
              GridView1.AutoGenerateSelectButton() = False
              GridView1.DataBind()
  
  
              'Render the GridView1 as HTML - this will cause an error that will fire the VerifyRenderingInServerForm event -- this event is trapped by the Overriding sub procedure given at the end of the program listing
              GridView1.RenderControl(htw)
  
              'Write the response
              Response.Write(sw.ToString())
              Response.End()
  
              'Turn sorting and paging on and rebind the GridView control
              GridView1.AllowSorting = True
              GridView1.AllowPaging = True
              '.GridView1.PageSize = 10
              GridView1.AutoGenerateSelectButton() = True
              GridView1.DataBind()
          End If
      Catch ex As Exception
  
      End Try
  
  End Sub
  </code></pre>