Select Excel 数据源的语句
Select Statement for Excel Datasource
我连接到 Excel sheet,它充当数据库。我需要 select 一些带有 where 条件的记录,但出现错误:
No value given for one or more required parameters
通过使用以下代码:
Dim conn As Object
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Set conn = CreateObject("ADODB.Connection")
XLName = "C:\Users\X\Desktop\rawdata.xlsx"
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" &
XLName & "';Extended Properties='Excel 12.0;HDR=NO;IMEX=1';"
conn.Open connString
rs.Open ("SELECT * FROM [data$] where industry='Government'"), conn,
adOpenDynamic, adLockReadOnly
Sheet1.Range("A2").CopyFromRecordset rs
rs.Close
conn.Close
当您设置 HDR=NO 时,excel table 中的列标题将被忽略,并将使用内部名称。查看较早的答案:c#, oledb connection string issue
我连接到 Excel sheet,它充当数据库。我需要 select 一些带有 where 条件的记录,但出现错误:
No value given for one or more required parameters
通过使用以下代码:
Dim conn As Object
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Set conn = CreateObject("ADODB.Connection")
XLName = "C:\Users\X\Desktop\rawdata.xlsx"
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" &
XLName & "';Extended Properties='Excel 12.0;HDR=NO;IMEX=1';"
conn.Open connString
rs.Open ("SELECT * FROM [data$] where industry='Government'"), conn,
adOpenDynamic, adLockReadOnly
Sheet1.Range("A2").CopyFromRecordset rs
rs.Close
conn.Close
当您设置 HDR=NO 时,excel table 中的列标题将被忽略,并将使用内部名称。查看较早的答案:c#, oledb connection string issue