将表导出到同一目录中的 Excel 电子表格

发布于 2024-08-13 17:32:50 字数 417 浏览 2 评论 0原文

我的 Access 数据库中有两个表,我希望能够将它们导出到 Excel

我可以通过打开表格,然后执行“文件”->“导出...”,然后选择格式并输入文件名来完成此操作。

然而,通过这种方式,用户实际上必须输入名称,因此存在错误命名文件或将其保存为错误格式或保存在错误位置的空间。此外,这两个表必须导出到两个单独的工作簿。

我想要做的是在表单上创建一个按钮,自动将一个表导出到一个工作表,将另一个表导出到另一个工作表,两者都在同一个 Excel 工作簿中。

如果无法将它们放在同一工作簿中,也没关系。我只是希望它们自动导出到保存我的访问数据库的同一目录中。

如果您知道如何执行此操作,则一个额外的好处可能是自定义名称以包含日期。这样目录也会有历史导出。有什么建议吗?

I have two tables in my access database that I want to be able to export to excel.

I can do it by opening the table and then doing File->Export... and then choosing the format and typing in the file name.

However, this way the user actually has to type the name in so there is room for misnaming the file or for saving it as the wrong format or in the wrong location. Also, the two tables have to be exported to two separate workbooks.

What I want to be able to do is make a button on a form that automatically exports one table to one worksheet and the other to another worksheet, both in the same excel workbook.

If putting them in the same workbook isn't possible, that's fine. I just want them to automatically be exported to the same directory my access database is saved in.

If you know how to do it, an added perk might be to customize the name to include the date. That way the directory would have historical exports as well. Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最近可好 2024-08-20 17:32:50

您可以使用 VBA 将 Access 数据库表导出为 Excel 工作簿中的工作表。

要获取 Access 数据库的路径,请使用 CurrentProject.Path 属性。

要使用当前日期命名 Excel 工作簿文件,请使用 Format(Date, "yyyyMMdd") 方法。

最后,要将表导出为工作表,请使用 DoCmd.TransferSpreadsheet 方法。

示例:

Dim outputFileName As String
outputFileName = CurrentProject.Path & "\Export_" & Format(Date, "yyyyMMdd") & ".xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table1", outputFileName , True
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table2", outputFileName , True

这会将 Table1 和 Table2 输出到同一工作簿中。

华泰

You can use VBA to export an Access database table as a Worksheet in an Excel Workbook.

To obtain the path of the Access database, use the CurrentProject.Path property.

To name the Excel Workbook file with the current date, use the Format(Date, "yyyyMMdd") method.

Finally, to export the table as a Worksheet, use the DoCmd.TransferSpreadsheet method.

Example:

Dim outputFileName As String
outputFileName = CurrentProject.Path & "\Export_" & Format(Date, "yyyyMMdd") & ".xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table1", outputFileName , True
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table2", outputFileName , True

This will output both Table1 and Table2 into the same Workbook.

HTH

小女人ら 2024-08-20 17:32:50

Lawrence has given you a good answer. But if you want more control over what gets exported to where in Excel see Modules: Sample Excel Automation - cell by cell which is slow and Modules: Transferring Records to Excel with Automation You can do things such as export the recordset starting in row 2 and insert custom text in row 1. As well as any custom formatting required.

温暖的光 2024-08-20 17:32:50

对于通过搜索引擎找到此内容的人,您不需要 VBA。你可以:

1.) select the query or table with your mouse
2.) click export data from the ribbon
3.) click excel from the export subgroup
4.) follow the wizard to select the output file and location.

For people who find this via search engines, you do not need VBA. You can just:

1.) select the query or table with your mouse
2.) click export data from the ribbon
3.) click excel from the export subgroup
4.) follow the wizard to select the output file and location.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文