我正在尝试使用ADO连接从已关闭的Excel 2007工作簿(.xlsx)复制数据。
我有连接字符串工作。但是当我尝试在Recordset中打开命令时(第二行到最后一行),我收到了自动化错误。
以下代码可能不太清楚:
“wsSummary”是一个工作表对象 “strSourceFile”是一个字符串,其中包含我需要复制的目标数据(例如Template.xlsx)
strSourceFile = wsSummary.Cells(nFirstRow + 4, 7)
strSheetSource = "Sheet1"
strSQL = "SELECT * FROM [" & strSheetSource & "]"
Set dbConnection = New ADODB.Connection
With dbConnection
.Provider = "Microsoft.ACE.OLEDB.12.0;"
.connectionString = "Data Source=" & strPOINTDataPath & strSourceFile & _
";Extended Properties=""Excel 12.0 Xml;HDR=NO;IMEX=1"";"
.ConnectionTimeout = 40
.Open
End With
If dbConnection = "" Then GoTo ErrorText
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = dbConnection
.CommandText = strSQL
End With
Set rs = New ADODB.Recordset
With rs
.ActiveConnection = dbConnection
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
.Open cmd
End With
答案 0 :(得分:1)
我认为您在SQL语句中错过了$ character
。尝试将适当的行改为这一行:
strSQL = "SELECT * FROM [" & strSheetSource & "$]"
或将strSheetSource variable
更改为:
strSheetSource = "Sheet1$"