简体   繁体   English

如何以编程方式将Excel数据导入Access表?

[英]How can I programmatically import Excel data into an Access table?

I've read through a bit of the related threads, but still left me with this question. 我已经阅读了一些相关主题,但是仍然给我留下了这个问题。 I want to write a function in an Access database application to programmatically import Excel data starting before the first two rows—which are the header and the unit delimiters. 我想在Access数据库应用程序中编写一个函数,以编程方式导入Excel数据,该数据从头两行(头和单位定界符)开始。

I am looking to accomplish the following things: 我希望完成以下任务:

  • Being able to dynamically select the Excel file I am looking to import, perhaps using a dialog box and perhaps a file browser window. 能够动态选择我要导入的Excel文件,可能使用对话框或文件浏览器窗口。
  • Insert 'common' data into each row as it's imported - like the asset number of the recorder and the recorder's designated location. 在导入的每一行中插入“常用”数据-例如记录仪的资产编号和记录仪的指定位置。
  • Start the import at row #3, instead of row #1 - as the device automatically puts the header and unit of measurement information for the record up there. 从第3行开始导入,而不是从第1行开始-设备会自动在其中放置记录的标头和度量单位信息。
  • Ignore all other columns in the worksheet - the data will ALWAYS be present in columns A through G, and data will ALWAYS begin on row #3. 忽略工作表中的所有其他列-数据将始终存在于A到G列中,并且数据将始终从#3行开始。

This is how the Excel data is commonly formatted (the dashes represent the data): 这是通常格式化Excel数据的方式(破折号代表数据):

Date     Time     Temp     Dew Point     Wet Bulb     GPP     RH
                       Cº       Cº            Cº           g/Kg    %
     ----     ----     ----     ----          ----         ----    ----
     ----     ----     ----     ----          ----         ----    ----

I've tried the built-in Access 'Get External Data' function, but it won't skip beyond row #2 and the extra data in the Excel file throws an error when trying to import, stopping the process in its tracks. 我已经尝试了内置的Access“获取外部数据”功能,但不会跳过第2行,并且在尝试导入时Excel文件中的多余数据会引发错误,从而使过程停止在其轨道上。

I'll be the first to admit that I have never tried to write a import function for Access before using external files, hence I am a bit of a newbie. 我将是第一个承认我在使用外部文件之前从未尝试为Access编写导入功能的人,因此我是个新手。 Any help people can show me will always be greatly appreciated, and I can update this with attempted code as necessary. 人们可以向我显示的任何帮助将始终受到赞赏,我可以根据需要使用尝试的代码进行更新。 Thank you in advance for all of your help, everyone! 预先感谢大家的帮助!

-- Edited 01/03/2011 @ 10:41 am -- -编辑01/03/2011 @ 10:41-

After reading the ADO connection to Excel data thread proposed by Remou, here is some code I think might do the job, but I am not sure. 读完Remou提出的与Excel数据线程的ADO连接后,这里是一些我认为可以完成此工作的代码,但是我不确定。

Dim rs2 As New ADODB.Recordset
Dim cnn2 As New ADODB.Connection
Dim cmd2 As New ADODB.Command
Dim intField As Integer
Dim strFile As String

strFile = fncOpenFile
If strFile = "" Then Exit Sub

With cnn2
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = "Data Source='" & strFile & "'; " & "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
    .Open
End With

Set cmd2.ActiveConnection = cnn2
cmd2.CommandType = adCmdText
cmd2.CommandText = "SELECT * FROM [Data$] WHERE G1 IS NOT NULL"
rs2.CursorLocation = adUseClient
rs2.CursorType = adOpenDynamic
rs2.LockType = adLockOptimistic

rs2.Open cmd2

You can use TransferSpreadsheet : http://msdn.microsoft.com/en-us/library/aa220766(v=office.11).aspx 您可以使用TransferSpreadsheet: http : //msdn.microsoft.com/zh-cn/library/aa220766(v=office.11​​).aspx

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, _
    "Employees","C:\Data\Test.xls", True, "A3:G12"

Or you can connect to Excel with an ADO connection . 或者,您可以使用ADO连接连接到Excel

It may be simplest to import or link and then use a query to update the relevant table with the spreadsheet data and the common data. 可能最简单的方法是导入或链接,然后使用查询用电子表格数据和公共数据更新相关表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM