c++ - 使 excel 在单个打印作业中打印多个工作表

标签 c++ excel com printing office-automation

我正在使用 COM 自动化打开 xls 文件并将其打印到虚拟 PDF 打印机。

Sheets sheets(m_Application.GetSheets());
sheets.PrintOut(CovOptional,CovOptional,CovOptional,CovOptional,COleVariant(_T("My PDF Printer")),CovOptional,CovOptional);

但是,如果文件有多个工作表,excel 会为每个工作表创建一个单独的打印作业,所以我会得到多个 PDF 文件而不是一个。

如何强制它在单个打印作业中打印所有页面?

最佳答案

我找到了一个很好的解释来解释为什么 Excel 有这种行为 in this article .来自文章

This happens because of the way Microsoft Excel sends the print job. Excel assumes that all your individual sheets have different page setups, so it sends them as multiple print-jobs.

In order to have all the individual sheets printed within a single PDF file (not multiple PDFs) you need to set the same page setup options for all of them (page setup in Excel modifies the print size of the resulting file, so for example if you set the page layout to landscape in Excel, the printed page will be landscape but your original Excel file will still maintain the same view).

我改编了来自 an answer 的代码到一个类似的问题。代码最初是用 C# 编写的,但我将其改编为 C++。

Sheets sheets(m_Application.GetSheets());

for (int ii = 1; ii <= sheets.GetCount(); ii++)
{
    _Worksheet sheet(sheets.GetItem(COleVariant((long) ii)));
    PageSetup pagesetup(sheet.GetPageSetup());
    pagesetup.SetOrientation(2); // landscape
    pagesetup.SetOrder(1); // xlDownThenOver
    pagesetup.SetFitToPagesWide(COleVariant((long) 1));
    pagesetup.SetFitToPagesTall(COleVariant((long) 50));
    VARIANT variant;
    variant.vt = VT_BOOL;
    variant.boolVal = VARIANT_FALSE;
    pagesetup.SetZoom(variant);
}

sheets.PrintOut(CovOptional, CovOptional, CovOptional, CovOptional, COleVariant(_T(MyAppPrinterName)), CovOptional, CovOptional);

关于c++ - 使 excel 在单个打印作业中打印多个工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12842221/

相关文章:

c++ - C和C++之间的Linux共享内存是否可行?

c++ - 如何确定哪些库是动态加载的?

java - 使用 Apache POI 从 Excel 读取单元格

r - 使用 R 下载 Excel 文件

用于读取 Microsoft Excel 文件的 Java 库

c++ - 无法包含 DirectShow 示例并对其进行编译 (PushSourceDesktop)

c++ - 网格中的空心方 block

c++ - 访问 "parent"或 "owner"函数 C++

python - 我可以在 VBA 中使用我的 python 自定义对象吗?

asp.net - 使用经典 ASP 从 COM 对象中检索字符串数组