简体   繁体   English

从 Access 导出到 Excel

[英]Export From Access to Excel

I want to export some data in one recordset in Access to Excel.我想在 Access to Excel 的一个记录集中导出一些数据。

I know the DoCmd.TransferSpreadsheet command but it works only with stored queries, and in my case it's a runtime-filtered recordset.我知道 DoCmd.TransferSpreadsheet 命令,但它只适用于存储的查询,在我的例子中,它是一个运行时过滤的记录集。

I've tried some codes to do what I want.我已经尝试了一些代码来做我想做的事。 I can get the data exported but I cannot get the column name from the recordset.我可以导出数据,但无法从记录集中获取列名。

Any suggestion on the commands or how to get those column names from recordset?关于命令或如何从记录集中获取这些列名的任何建议?

DAO recordsets have a name property you can use. DAO 记录集有一个可以使用的name属性。

Dim rs As DAO.Recordset

Set rs = CurrentDb.OpenRecordset("SELECT * FROM ARTIKELGRUPPE")
Debug.Print rs.Fields(0).Name
Debug.Print rs.Fields(1).Name

Output for my table: Output 我的桌子:

id
Name

You can alter a stored query prior to calling transfer spreadsheet您可以在调用传输电子表格之前更改存储的查询

Dim myQuery As QueryDef
Set myQuery = CurrentDb.QueryDefs("SampleQuery")
myQuery.SQL = "SELECT * FROM myTable WHERE something"
DoCmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcel9, "SampleQuery", "c:\test.xls"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM