Excel多个工作表来访问表

时间:2016-05-25 14:25:44

标签: database excel excel-vba ms-access access-vba vba

我收到了一个Excel文件.xlsx中某人的数据库转储,其中包含50多个工作表。为了让我理解它,我必须将它上传到数据库并开始一起提取一些信息。

我对Access女士感到更舒服,所以暂时不要使用它。

如何在Access db?

中将包含所有50个工作表的单个文件分别导入50个表中?

在Access中使用简单的外部数据选项时,有人可以提供帮助,因为我收到错误消息。

2 个答案:

答案 0 :(得分:0)

使用Excel.Application对象,从Excel Workbook获取Worksheets集合中的所有名称,并在工作簿中注册所有工作表名称:

Dim App As Object                                            'Excel interface
Dim File As Object                                           'Your file     
Dim ws As Variant                                             'Your worksheet

Set App = CreateObject("Excel.Application")                  'This opens excel application windows
Set File = App.Workbooks.Open(fileName)                      'This opens your particular excel file (or workbook)

For each ws In File
    'Register Name in a table named tblSheetNames, Containing a field named 'ShtNm': 
    CurrentDb.Execute "INSERT INTO tblSheetNames(ShtNm) VALUES ('" & ws.Name & "')"
Next
'Close the App to allow link to the Excel file
App.Close: Set App = Nothing

然后导入所有这些工作表:

'Import all these worksheets:
Dim rst as DAO.Recordset
Set rst = CurrentDb.OpenRecordset("tblSheetNames")
Do While Not rst.Eof
    'Link to SpreadSheet, **make sure you add the '!' to the spread-sheet-name for the *Range* parameter**
    DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel12, "Temp", fileName, rst!ShtNm & "!"
    'Dump sheet into table 'MyTable' and then Delete Link:
    CurrenDb.TableDefs.Delete("Temp")
rst.MoveNext: Loop

add the '!' to the spread-sheet-name for the Range parameter

答案 1 :(得分:0)

'通过TransferSpreadsheet(VBA)从单个文件夹中的所有EXCEL文件导入数据'

Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean

' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = False

' Replace C:\Documents\ with the real path to the folder that
' contains the EXCEL files
strPath = "C:\Documents\"

' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "tablename"

strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
      strPathFile = strPath & strFile
      DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
            strTable, strPathFile, blnHasFieldNames

' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
'       Kill strPathFile

      strFile = Dir()
Loop

这个想法来自这里。

http://www.accessmvp.com/KDSnell/EXCEL_Import.htm#ImpFolderFiles

查看链接。你会在那里找到其他几个非常酷和功利的概念。