使用数据库中的信息创建Excel文件

使用数据库中的信息创建Excel文件,excel,vb.net,for-loop,Excel,Vb.net,For Loop,我有一个返回如下内容的查询: 1 2 3 4 5 6 7 A. B. C. D. E. F. G. Etc... Etc... N rows Sql=“select * from table” Dim cmd As New SqlDataAdapter(Sql, con) Dim ds As New DataSet cmd.Fill(ds) For i=0 To Tables(0).Rows.Count - 1 For x=

我有一个返回如下内容的查询:

1    2    3    4    5    6    7
A.   B.   C.   D.   E.   F.   G.
Etc...
Etc...
N rows
Sql=“select * from table”
Dim cmd As New SqlDataAdapter(Sql, con)
Dim ds As New DataSet
cmd.Fill(ds)

For i=0 To Tables(0).Rows.Count - 1
    For x=0 To ds.Tables(0).Columns.Count - 1
        ExcelFile.Cells(i+1;x+1)=ds.Tables(0).Rows(i).Item(j)
    Next
Next
我将查询存储在数据集上。然后,我使用如下方式创建Excel文件:

1    2    3    4    5    6    7
A.   B.   C.   D.   E.   F.   G.
Etc...
Etc...
N rows
Sql=“select * from table”
Dim cmd As New SqlDataAdapter(Sql, con)
Dim ds As New DataSet
cmd.Fill(ds)

For i=0 To Tables(0).Rows.Count - 1
    For x=0 To ds.Tables(0).Columns.Count - 1
        ExcelFile.Cells(i+1;x+1)=ds.Tables(0).Rows(i).Item(j)
    Next
Next
代码运行良好,除了我还需要编写列标题名称1、2、3、4等。我的第一个问题是如何添加标题

主要问题是。。。查询有时会返回超过80k的结果,因此按照for循环逻辑,我的代码将为每一列运行80k次,在这种情况下是7次,这将给我一个缓慢的结果

还有另一种快速填写Excel文件的方法吗?或者这是最好的方法?

我使用以下方法:

Dim sSql As String
Dim tbl As ListObject

'Declare a Connection object
Dim cnDB As New ADODB.Connection

'Declare a Recordset Object
Dim rs As ADODB.Recordset

' Housekeeping, set the connection strings
Set cnn = New ADODB.Connection
cnn.Provider = "MSDASQL"
cnn.CommandTimeout = 100

Set tbl = ActiveSheet.ListObjects("Lookup")
With tbl.DataBodyRange
    If .Rows.Count > 1 Then
        .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Rows.Delete
    End If
End With

' Connect to the database and paste new data
cnn.ConnectionString = "driver={};server={};uid={};pwd={};database={}"
sSql = "SELECT BLAH BLAH "
cnn.Open
Set rs = cnn.Execute(sSql)

ThisWorkbook.Worksheets("Lookup").Range("A2").CopyFromRecordset rs
cnn.Close
您可以访问DataTable中每列的ColumnName属性。例如,要在尽可能少的修改代码的情况下添加标题,可以执行以下操作:

'Write ColumnName to the corresponding cell in row 1
For x=0 To ds.Tables(0).Columns.Count - 1
    ExcelFile.Cells(1, x+1) = ds.Tables(0).Columns(x).ColumnName
Next

'Modded to start at the second row and fix index variable
For i=1 To Tables(0).Rows.Count - 1
    For x=0 To ds.Tables(0).Columns.Count - 1
        ExcelFile.Cells(i+1, x+1) = ds.Tables(0).Rows(i).Item(x)
    Next
Next
不过,你担心这一点是对的。Excel自动化的首要规则是尽可能少地与Excel交互,因为每次交互都非常昂贵

假设您使用的是常规的Office互操作,那么应该构建一个表示查询中的值的二维数组。然后在工作表中找到一个等效的大小范围,并将该范围的值设置为数组。通过这种方式,您将数千次交互切割为一次

Dim rowCount = ds.Tables(0).Rows.Count
Dim colCount = ds.Tables(0).Columns.Count

Dim ws = ExcelFile

Dim valueSet(,) As Object
ReDim valueSet(rowCount - 1, colCount - 1)

For row = 0 To rowCount - 1
    For col = 0 To colCount - 1
        valueSet(row, col) = ds.Tables(0).Rows(row).Item(col)
    Next
Next

'Set the entire set of values in a single operation
ws.Range(ws.Cells(1, 0), ws.Cells(rowCount, colCount).Value = valueSet
此外,如果您实际上正在使用Excel Interop或类似的包装器,您应该查看它是否满足您的需要。它是一个帮助程序库,可与OfficeOpenXML一起使用,甚至不需要安装Excel