vba 访问 outputTo Excel 模板和格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15282448/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:54:08  来源:igfitidea点击:

Access outputTo Excel template and formatting

excelvbams-accessexportaccess-vba

提问by MAW74656

So I'm using VBA code to export a query to excel. This works, but the resulting file is ugly, and I'm trying to apply some simple formatting to it (thousands separators and dollar signs, column width, etc). I've seen code to do it manually in VBA, but this is ugly and will be difficult to change in the future, so I'm looking at using an Excel template file instead.

所以我使用 VBA 代码将查询导出到 excel。这有效,但生成的文件很难看,我正在尝试对其应用一些简单的格式(千位分隔符和美元符号、列宽等)。我已经看到在 VBA 中手动完成的代码,但这很丑陋,将来很难更改,所以我正在考虑使用 Excel 模板文件。

My problem is that setting a template file seems to have no effect on the resulting spreadsheet. What are template files supposed to do here? What other ways are there to apply simple formatting to this spreadsheet?

我的问题是设置模板文件似乎对生成的电子表格没有影响。模板文件在这里应该做什么?还有哪些其他方法可以将简单格式应用于此电子表格?

 DoCmd.OutputTo acOutputQuery, QueryName, "ExcelWorkbook(*.xlsx)", FileName & ".xlsx", OpenFile, template, 0, acExportQualityPrint

回答by RichardC

You can't use a template file when using OutputTo with Excel. From the MSDN documentation:

在 Excel 中使用 OutputTo 时不能使用模板文件。从MSDN 文档

DoCmd.OutputTo Parameters

  • Name: TemplateFile
  • Required/Optional: Optional
  • Data Type: Variant
  • Description: A string expression that's the full name, including the path, of the file you want to use as a template for an HTML, HTX, or ASP file.

DoCmd.OutputTo 参数

  • 名称:模板文件
  • 必需/可选:可选
  • 数据类型:变体
  • 说明:一个字符串表达式,它是您要用作HTML、HTX 或 ASP 文件模板的文件的全名,包括路径

If you want to format your output, you will probably have to either do it manually in VBA (which can be a hassle, as you mentioned) or maybe get a third-party control/library/class that can do it for you, however I'm not aware of any personally.

如果你想格式化你的输出,你可能不得不在 VBA 中手动完成(这可能很麻烦,正如你所提到的)或者可能获得一个可以为你完成的第三方控件/库/类,但是我个人不知道。