这是我的连接字符串
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=O:\[...].xlsx;Extended Properties='Excel 12.0 Xml;HDR=YES;'"
我直接从excel中动态提取工作表名称:
Dim xlWorkSheet As Excel.Worksheet
Dim xlApp As New Excel.Application
xlApp.Workbooks.Open("O:[...].xlsx", 0, True)
xlWorkSheet = CType(xlApp.Sheets(1), Excel.Worksheet)
Dim sheetName As String = xlWorkSheet.Name
Dim query As String = "SELECT [Part Number] FROM [" & sheetName & "$] WHERE [Sub-Category] = '-EEPROM'"
Dim tempTable As New DataTable()
然后是经典的OleDB交易:
Try ' OleDB transaction:
Using co As New OleDbConnection(connString)
Dim command As New OleDbCommand(query, co)
' Opens the connection
co.Open()
' Perform the 1st query:
Dim adapter As New OleDbDataAdapter(command)
' Fills in a table
tempTable.Clear()
adapter.Fill(tempTable)
' Disposes of objects
adapter.Dispose()
End Using
Catch ex As Exception
MsgBox("Error: " & ex.Message,, "Error getting the EEPROM match")
End Try
Aaaand我收到以下错误: ' XXX'不是有效的名称。确保它不包含无效字符或标点符号,并且不会太长。
xxx是工作表的实际名称,如excel中所示并通过excel对象检索。
编辑:此名称仅为字母和空格,长度为13个字符。
有点困惑,谢谢你的帮助!