将 Excel 文件读入 Access 数据库的最佳方法
从 Access 2007 应用程序中读取(仅读取)Excel 文件的“最佳”方法是什么?我只想循环遍历行并将数据放入 Access 表中。
我不想手动导入(“获取外部数据”对话框),而是通过 VBA 进行导入。用户获得一个带有“浏览”按钮的表单,然后指向一个具有已定义内容/格式的 Excel 文件。之后,VBA 代码读取数据并将其放入 Access 数据库中。
What's the "best" way to read (just read) an Excel file from within an Access 2007 application. I only want to loop trough the rows and put the data into an Access table.
I don't want a manually import (Get External Data dialog) but by VBA. The user gets a Form with a Browse button and then points to a Excel file with a defined content/format. After that the VBA code reads the data and puts it into the Access database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试 DoCmd.TransferSpreadsheet 方法。
这会将电子表格数据导入到名为 from_excel 的表中,并假设电子表格的第一行包含字段名称。请参阅 TransferSpreadsheet 的访问帮助或在线访问此处,了解更多详情。
You could try the DoCmd.TransferSpreadsheet method.
That imports spreadsheet data into a table named from_excel, and assumes the first row of the spreadsheet contains field names. See Access help for TransferSpreadsheet or online here, for more details.
如果要读取整个电子表格,可以将 Excel 电子表格直接导入到 Access 中。请参阅此处或此处。
您还可以选择链接到 Excel 电子表格而不是导入它。这样,对 Excel 电子表格的任何更改都将反映在链接表中。但是,您将无法在 Access 中进行更改。
第三种选择是在 Access 中编写一些 VBA 代码来打开记录集并读取电子表格。请参阅 KeithG 在 此线程。您可以执行以下操作在 VBA 中打开电子表格:
If you want to read the entire spreadsheet in, you can import an Excel spreadsheet directly into Access. See here or here.
You can also choose to link to the Excel spreadsheet instead of importing it. That way any changes to the Excel spreadsheet will be reflected in the linked table. However, you won't be able to make changes from within Access.
A third option is to write some VBA code within Access to open a recordset and read the spreadsheet in. See the answers from KeithG in this thread. You can do something like this to open the spreadsheet in VBA:
尝试这样的操作:
然后循环遍历行和列,从单元格中提取数据,并将其插入数据库中。 (您可以使用 worksheet.cells 方法。)尝试在 google 上搜索代码示例。
Try something like this:
And then loop through the rows and columns, pull the data from the cells, and insert it into the database. (You can use the worksheet.cells method.) Try searching on google for code samples.
下面是我读取excel文件和所有工作表名称的方法:
现在,您可以使用工作表的名称来读取整个内容:
Hereafter my method to read an excel file and all the worksheet names:
Now, you can use the name of the worksheet to read the whole content: