vba 如何将 Access 数据库中的所有表导出到 Excel - 每个表的工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4036772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to export all tables from an Access Database into Excel - A sheet for each table
提问by Albert
I have an Access database with ~30 tables.
我有一个包含约 30 个表的 Access 数据库。
How can I export all 30 tables into separate sheets in an Excel workbook?
如何将所有 30 个表导出到 Excel 工作簿中的单独工作表中?
I'm hoping to find some VBA/VBS code which I can run from within Access to accomplish this task.
我希望找到一些 VBA/VBS 代码,我可以在 Access 中运行这些代码来完成此任务。
Any ideas?
有任何想法吗?
回答by BenV
You should be able to do something like this:
你应该能够做这样的事情:
Dim tbl as Tabledef
For Each tbl in Currentdb.TableDefs
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, tbl.name, "PathName.xls", True, tbl.name
Next
The second tbl.nameis the worksheet name.
第二个tbl.name是工作表名称。
回答by Albert
Here's the full module I used.
这是我使用的完整模块。
Sub expotT()
Dim td As DAO.TableDef, db As DAO.Database
Set db = CurrentDb()
For Each td In db.TableDefs
If Left(td.Name, 4) <> "msys" Then
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
td.Name, "C:\xExcelTables.xls", True, td.Name
End If
Next
End Sub
回答by corysus
Here is formated and fixed version of above code. We don't need MSys tables in excel file and dbo prefix in sheet names. Export also can be made relative to MS Access db or fixed.
这是上述代码的格式化和固定版本。我们不需要 Excel 文件中的 MSys 表和工作表名称中的 dbo 前缀。导出也可以相对于 MS Access db 进行或固定。
Here is code:
这是代码:
Sub exportTables2XLS()
Dim td As DAO.TableDef, db As DAO.Database
Dim out_file As String
out_file = CurrentProject.path & "\excel_out.xls"
Set db = CurrentDb()
For Each td In db.TableDefs
If Left(td.Name, 4) = "MSys" Then
'// do nothing -- skip
Else
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
td.Name, out_file, True, Replace(td.Name, "dbo_", "")
End If
Next
End Sub

