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

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

问题描述

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

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

我可以通过打开表,然后执行File-> Export ...,然后选择格式并键入文件名来做到这一点.

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

这会将表1和表2输出到同一工作簿中.

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

HTH

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

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