如果您有一个明确定义的数据表布局(HansUp答案),ADO会很好地工作。如果您在加载对象之前需要添加控件,则可以挂钩到Excel工作簿,然后提取您喜欢的任何数据。这真的取决于你需要的控制水平。
Public Sub LoadExcelToAccess(xlPath As String)
'uses late binding to open excel workbook and open it line by line
'make reference to Microsoft Excel xx.x Object Model to use native functions, requires early binding however
Dim xlApp As Object 'Excel.Application
Dim xlWrk As Object 'Excel.Workbook
Dim xlSheet As Object 'Excel.Worksheet
Dim i As Long
Dim sql As String
Set xlApp = VBA.CreateObject("Excel.Application")
'toggle visibility for debugging
xlApp.Visible = False
Set xlWrk = xlApp.Workbooks.Open(xlPath)
Set xlSheet = xlWrk.Sheets("Sheet1") 'modify to your perticular sheet
'depends on what your trying to do with the sheet
For i = 1 To 10
'quick and dirty: best to load items into custom class collection, then do processing there
sql = "Insert Into [Temp] (Col1) VALUES (" & xlSheet.Cells(i, 1).Value & ")"
DoCmd.RunSQL sql
Next i
'make sure to dispose of objects
xlWrk.Close
xlApp.Quit
Set xlSheet = Nothing
Set xlWrk = Nothing
Set xlApp = Nothing
End Sub
是否可以将xlsx文件导出为csv,然后逐行读取它? – 2011-03-15 12:32:46
@MPękalski可能! – 2011-03-15 12:50:55
将您的电子表格限制为仅一条记录,并查看它是否可用。 – JeffO 2011-03-15 17:42:55