将文件导入到microsoft access:字段映射

时间:2017-03-02 19:41:16

标签: excel csv ms-access import

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

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

我直接导入Access的第一个选项卡并创建了一个新表。即使其他文件具有相同的布局,我也似乎无法正确导入其他文件。即使字段具有完全相同的名称,但它仍然会阻止映射。

不是很糟糕,我要么得到一两个字段的类型转换错误(由于访问表中的所有字段都是"短文本和#34所以我也没有得到;所以我可以只导入数据文件中的任何内容而不进行处理)或者文件中的一些错误的源字段被导入到数据库中错误的目标字段中。

只是几个字段搞砸了几乎令人讨厌,因为这意味着我必须检查整个表格以确定事情是否已经消失。并且它不一致,每次尝试时它都会有所不同。

我尝试从excel文件导入数据,并将每个标签保存为csv。什么都行不通。 WTF我做错了。很高兴尝试使用其他一些数据库(filemaker等)。我不在乎使用访问权限,我只是觉得它会更容易,但我不知道为什么这种情况非常困难。

1 个答案:

答案 0 :(得分:0)

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

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

相关问题