title: Visual Basic - Writing a Datatable to Excel
body: <p>I am working on a console application that will grab all of the .sql files in a specific folder, use those to query a database, and then export the results of each to an excel file. I've gotten everything down up to the datatable. I am asking for any advice or guidance on getting that datatable to excel. Any help would be appreciated. Thank you.  </p>
  
  <pre><code>Imports System.Data.OleDb
  </code></pre>
  
  <hr>
  
  <pre><code>Module SqlExport
  
  Sub Main()
  
      Dim SQLString As String
      Dim SQLDirectory As New IO.DirectoryInfo("\\Datastore\scripts\SQL")
      Dim SQLQueries As IO.FileInfo() = SQLDirectory.GetFiles("*.sql")
      Dim CurrentQuery As IO.FileInfo
  
      Dim dt As New DataTable
  
      For Each CurrentQuery In SQLQueries
          SQLString = System.IO.File.ReadAllText(CurrentQuery.FullName)
  
          Using connection As New OleDb.OleDbConnection("provider=advantage ole db provider;data source=\\database\dba;advantage server type=ads_remote_server;trimtrailingspaces=true;")
  
              Dim command As New OleDbCommand(SQLString, connection)
              Dim da As New OleDb.OleDbDataAdapter(command)
  
              da.Fill(dt)
  
          End Using
  
      Next
  
  End Sub
  
  End Module
  </code></pre>