将表导出到同一目录中的 Excel 电子表格
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 VBA 将 Access 数据库表导出为 Excel 工作簿中的工作表。
要获取 Access 数据库的路径,请使用
CurrentProject.Path
属性。要使用当前日期命名 Excel 工作簿文件,请使用
Format(Date, "yyyyMMdd")
方法。最后,要将表导出为工作表,请使用
DoCmd.TransferSpreadsheet
方法。示例:
这会将 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:
This will output both Table1 and Table2 into the same Workbook.
HTH
劳伦斯给了你一个很好的答案。但是,如果您想更好地控制导出到 Excel 中的位置的内容,请参阅模块:示例 Excel 自动化< /a> - 逐个单元,速度较慢,模块:自动将记录传输到 Excel 您可以执行一些操作,例如导出从第 2 行开始的记录集并在第 1 行中插入自定义文本。以及所需的任何自定义格式。
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.
对于通过搜索引擎找到此内容的人,您不需要 VBA。你可以:
For people who find this via search engines, you do not need VBA. You can just: