title: Export DataTable to Excel from VB.Net
body: <p>The following code is my current attempt at opening some data in excel from a website button in VB.Net. I would like the data to show up barebones, but the formatting from the table on the website always follows. The paging and colors make the data near impossible to read and can only see the first page of data. Any quick fixes? I've tried a lot of things I've found on here but to no avail.   </p>
  
  <pre><code> Private Sub DownloadExcel()
  
      Response.Clear()
  
      'Dim dt As DataTable = TryCast(ViewState("GridData"), DataTable)
  
      Grid_Bad_Meters.AllowPaging = False
      Grid_Bad_Meters.AllowSorting = False
  
      'Grid_Bad_Meters.DataSource = dt
      'Grid_Bad_Meters.DataBind()
  
      Dim sfile As String = "Communication_Failures" &amp; Now.Ticks
  
      Response.AddHeader("content-disposition", "attachment;filename=" &amp; sfile &amp; ".xls")
      Response.Charset = ""
  
      ' If you want the option to open the Excel file without saving then 
      ' comment out the line below 
      ' Response.Cache.SetCacheability(HttpCacheability.NoCache); 
      Response.ContentType = "application/vnd.ms-excel"
      Dim stringWrite As New System.IO.StringWriter()
      Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
      Grid_Bad_Meters.RenderControl(htmlWrite)
      Response.Write(stringWrite.ToString())
      Response.End()
  
      'Grid_Bad_Meters.AllowPaging = True
      'Grid_Bad_Meters.AllowSorting = True
      'GridView1.DataSource = dt
  
      'GridView1.DataBind()
  
  
  End Sub
  </code></pre>