【问题标题】:Import file into microsoft access: field mapping将文件导入microsoft access:字段映射
【发布时间】:2017-07-22 17:07:51
【问题描述】:

这让我发疯了。我一直在反对将一些excel数据导入微软访问。愚蠢的我认为这应该很容易,因为它们都是微软的产品。

共有三个大约 40MB 的 excel 文件。每个文件中有四个选项卡,每个选项卡在文件之间具有相同顺序的相同字段。即,文件 1 中的选项卡 A 具有与文件 2 和文件 3 中的选项卡 A 相同的顺序的字段名称。并且 access 数据库中的相应表与文件中的顺序完全相同的字段名称也完全相同.其他选项卡也是如此。每个选项卡中大约有 90K 行和大约 40 列。

我直接导入 Access 的第一个选项卡并创建了一个新表。即使其他文件具有相同的布局,我似乎也无法正确导入其他文件。即使这些字段以完全相同的顺序具有完全相同的名称,它也会不断搞砸映射。

不严重,我要么得到一个或两个字段的类型转换错误(我也没有得到,因为访问表中的所有字段都是“短文本”类型,所以我可以导入任何内容没有处理的数据文件)或文件中的几个错误源字段被导入到数据库中错误的目标字段中。

只有几个字段被弄乱了,这几乎更烦人,因为这意味着我必须检查整个表格才能确定事情是否发生了。而且它并不一致,每次我尝试它时都会以不同的方式搞砸。

我尝试从 excel 文件中导入数据,并将每个选项卡保存为 csv。没有任何效果。 WTF我做错了吗。很高兴尝试使用其他数据库(文件制作器等)。我不关心使用访问,我只是认为它会更容易,但我不明白为什么这会如此困难。

【问题讨论】:

    标签: excel csv ms-access import


    【解决方案1】:

    从文件夹中所有文件的所有工作表中导入数据。

    Dim blnHasFieldNames As Boolean, blnEXCEL As Boolean, blnReadOnly As Boolean
    Dim intWorkbookCounter As Integer
    Dim lngCount As Long
    Dim objExcel As Object, objWorkbook As Object
    Dim colWorksheets As Collection
    Dim strPath As String, strFile As String
    Dim strPassword As String
    
    ' Establish an EXCEL application object
    On Error Resume Next
    Set objExcel = GetObject(, "Excel.Application")
    If Err.Number <> 0 Then
          Set objExcel = CreateObject("Excel.Application")
          blnEXCEL = True
    End If
    Err.Clear
    On Error GoTo 0
    
    ' Change this next line to True if the first row in EXCEL worksheet
    ' has field names
    blnHasFieldNames = False
    
    ' Replace C:\MyFolder\ with the actual path to the folder that holds the EXCEL files
    strPath = "C:\MyFolder\"
    
    ' Replace passwordtext with the real password;
    ' if there is no password, replace it with vbNullString constant
    ' (e.g., strPassword = vbNullString)
    strPassword = "passwordtext"
    
    blnReadOnly = True ' open EXCEL file in read-only mode
    
    strFile = Dir(strPath & "*.xls")
    
    intWorkbookCounter = 0
    
    Do While strFile <> ""
    
          intWorkbookCounter = intWorkbookCounter + 1
    
          Set colWorksheets = New Collection
    
          Set objWorkbook = objExcel.Workbooks.Open(strPath & strFile, , _
                blnReadOnly, , strPassword)
    
          For lngCount = 1 To objWorkbook.Worksheets.Count
                colWorksheets.Add objWorkbook.Worksheets(lngCount).Name
          Next lngCount
    
          ' Close the EXCEL file without saving the file, and clean up the EXCEL objects
          objWorkbook.Close False
          Set objWorkbook = Nothing
    
          ' Import the data from each worksheet into a separate table
          For lngCount = colWorksheets.Count To 1 Step -1
                DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
                      "tbl" & colWorksheets(lngCount) & intWorkbookCounter, _
                      strPath & strFile, blnHasFieldNames, _
                      colWorksheets(lngCount) & "$"
          Next lngCount
    
          ' Delete the collection
          Set colWorksheets = Nothing
    
          ' Uncomment out the next code step if you want to delete the
          ' EXCEL file after it's been imported
          ' Kill strPath & strFile
    
          strFile = Dir()
    
    Loop
    
    If blnEXCEL = True Then objExcel.Quit
    Set objExcel = Nothing
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      相关资源
      最近更新 更多