【问题标题】:Import data from an Excel file to an Access table将数据从 Excel 文件导入 Access 表
【发布时间】:2019-11-28 05:38:22
【问题描述】:

我正在尝试从用户选择的 Excel 文件中导入数据,并将其数据导入到访问表中。

要求用户选择我使用此代码的文件

Private Function importarExcelTabla()
Dim excelMedi As Variant
Dim cuadroSeleccion As Office.FileDialog

Set cuadroSeleccion = Application.FileDialog(msoFileDialogFilePicker)
'Abre el cuadro de seleccion de ficheros

With cuadroSeleccion
.AllowMultiSelect = False
.Title = "Selecciona el archivo por favor"
.Filters.Clear
.Filters.Add "Todos los archivos", "*.*", 1


If .Show = True Then
excelMedi = cuadroSeleccion.SelectedItems(1)

选择后,我使用 transgerSpreadsheet 将 .xlsx 文件从范围导入到表中

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "MediPrueba", 
excelMedi, False, "A2:L950"

   End If

  End With

End Function

但我的问题是表格中没有填充 excel 数据,而且我从一个文件中放入了范围,但是:

¿可以选择不带第一行的所有文档,这样这将适用于其他长度的其他 excel 文件吗?

提前谢谢你

【问题讨论】:

    标签: excel vba ms-access


    【解决方案1】:

    实际上有从 www.accessmvp.com/KDSnell/EXCEL_Import.htm 查看此代码

    此代码通过选择一个起点(右上角)来工作,并一直工作到它遇到一个空白行并停止。要跳过第一行,请将起点设置为 A2

    
    Dim lngColumn As Long
    Dim xlx As Object, xlw As Object, xls As Object, xlc As Object
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Dim blnEXCEL As Boolean
    blnEXCEL = False
    
    ' Establish an EXCEL application object
    On Error Resume Next
    Set xlx = GetObject(, "Excel.Application")
    If Err.Number <> 0 Then
          Set xlx = CreateObject("Excel.Application")
          blnEXCEL = True
    End If
    Err.Clear
    On Error GoTo 0
    
    ' Change True to False if you do not want the workbook to be
    ' visible when the code is running
    xlx.Visible = True
    
    ' Replace C:\Filename.xls with the actual path and filename
    ' of the EXCEL file from which you will read the data
    Set xlw = xlx.Workbooks.Open("C:\Filename.xls", , True) ' opens in read-only mode
    
    ' Replace WorksheetName with the actual name of the worksheet
    ' in the EXCEL file
    Set xls = xlw.Worksheets("WorksheetName")
    
    ' Replace A1 with the cell reference from which the first data value
    ' (non-header information) is to be read
    Set xlc = xls.Range("A1") ' this is the first cell that contains data
    
    Set dbs = CurrentDb()
    
    ' Replace QueryOrTableName with the real name of the table or query
    ' that is to receive the data from the worksheet
    Set rst = dbs.OpenRecordset("QueryOrTableName", dbOpenDynaset, dbAppendOnly)
    
    ' write data to the recordset
    Do While xlc.Value <> ""
          rst.AddNew
                For lngColumn = 0 To rst.Fields.Count - 1
                      rst.Fields(lngColumn).Value = xlc.Offset(0, lngColumn).Value
                Next lngColumn
          rst.Update
          Set xlc = xlc.Offset(1,0)
    Loop
    
    rst.Close
    Set rst = Nothing
    
    dbs.Close
    Set dbs = Nothing
    
    ' Close the EXCEL file without saving the file, and clean up the EXCEL objects
    Set xlc = Nothing
    Set xls = Nothing
    xlw.Close False
    Set xlw = Nothing
    If blnEXCEL = True Then xlx.Quit
    Set xlx = Nothing
    

    【讨论】:

      猜你喜欢
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 2010-12-04
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      相关资源
      最近更新 更多