0

我有一个 xlsx 文件,其中有一些标题一直到第 6 行。我需要获取查询结果并将它们放入第 7 行以后的文件中。我不确定我是追加还是导出查询结果。

Const acExportQuery = 1
Set objAccess = CreateObject("Access.Application")
objAccess.OpenCurrentDatabase "\HealthyImmunity Contact Manager - V1.53.adp"
objAccess.ExportXML acAppendData, "c:\scripts\testOrder.xlsx", "col_USbatchOrders"

有人有想法么?

4

1 回答 1

0

Below is an example code which you can trigger from excel.. add it to workbook_open even in excel and it should import the query execution result from database

 Sub GetData()

 Dim cnn As ADODB.Connection

 Dim rs As ADODB.Recordset

 Dim sQRY As String

 Dim strFilePath As String

 strFilePath = "\HealthyImmunity Contact Manager - V1.53.adp"   
 Set cnn = New ADODB.Connection
 Set rs = New ADODB.Recordset
 Sheet1.Range("DataRange").ClearContents
 cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & strFilePath & ";"
 sQRY = "SELECT tblData.* FROM tblData"
 rs.CursorLocation = adUseClient
 rs.Open sQRY, cnn, adOpenStatic, adLockReadOnly
 Application.ScreenUpdating = False
 Sheet1.Range("B2").CopyFromRecordset rs
 rs.Close
 Set rs = Nothing
 cnn.Close
 Set cnn = Nothing
 Exit Sub

End Sub

于 2013-06-21T15:40:33.597 回答