[英]Visual Basic - Writing a Datatable to Excel
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. 我正在使用一个控制台应用程序,该应用程序将抓取特定文件夹中的所有.sql文件,使用它们来查询数据库,然后将每个结果导出到excel文件。 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.
谢谢。
Imports System.Data.OleDb
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
Thank you all for this. 谢谢大家 I ended up using the following to get the results I was looking for.
我最终使用以下命令来获得想要的结果。
Dim da As New OleDb.OleDbDataAdapter(command)
da.Fill(ds)
Dim Excel As Object = CreateObject("Excel.Application")
With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()
.Range("A1").select()
Dim i As Integer = 1
For col = 0 To ds.Tables(0).Columns.Count - 1
If Asc(ds.Tables(0).Columns(col).ColumnName) = 36 Then
.Columns(i).NumberFormat = "General"
.Cells(1, i).value = ds.Tables(0).Columns(col).ColumnName.Substring(1)
Else
.Columns(i).NumberFormat = "@"
.Cells(1, i).value = ds.Tables(0).Columns(col).ColumnName
End If
i += 1
Next
Dim j As Integer = 1
For col = 0 To ds.Tables(0).Columns.Count - 1
i = 2
For row = 0 To ds.Tables(0).Rows.Count - 1
.Cells(i, j).Value = ds.Tables(0).Rows(row).ItemArray(col).ToString
i += 1
Next
j += 1
Next
Dim fileName As String = Path.GetFileNameWithoutExtension(CurrentQuery.Name)
.ActiveWorkbook.SaveAs(Filename:="\\reports\" & value & "\" & fileName & DateTime.Now.ToString("yyyy-MM-dd") & ".xlsx", _
WriteResPassword:="123456", _
ReadOnlyRecommended:=False, _
CreateBackup:=False)
.Workbooks.Close()
.Quit()
End With
In Manual way .. 以手动方式..
First you have to create your XLS file and make colums header like fields in your database 首先,您必须创建XLS文件并在数据库中创建类似于字段的colums标头
Assumed that is "TRANSFER.XLS" .. 假设是“ TRANSFER.XLS” ..
Dim sFN = "Transfer.XLS"
Dim cnXLS As OleDbConnection
Dim cmdXLS As OleDbCommand
Dim dr as DataRow
cnXLS = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" & _
"data source=" & sFN & ";Extended Properties=Excel 8.0;")
cnXLS.Open()
For x as Integer = 0 to dt.Table(0).Rows.Count -1 '---> this is your dt
dr = dt.Table(0).Rows(x)
cmdXLS = New OleDbCommand("INSERT INTO [Sheet1$] ( name, .... ) VALUES ( dr.item("name", ....)", cnXLS)
cmdXLS.ExecuteNonQuery
Next
cnXLS.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.