将表格导出到同一目录中的 Excel 电子表格 [英] Export tables to an excel spreadsheet in same directory

查看:60
本文介绍了将表格导出到同一目录中的 Excel 电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的访问数据库中有两个表,我希望能够将它们导出到 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.

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

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?

推荐答案

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

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

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

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

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

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

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

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

示例:

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 输出到同一个工作簿中.

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

HTH

这篇关于将表格导出到同一目录中的 Excel 电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆