c# - Excel 到 PDF 导出 - 页码限制

标签 c# excel

我正在使用 C# 将 Excel 文件转换为 PDF。
这是我到目前为止的代码:

Excel.Application excelApp = new Excel.Application();
excelApp.Workbooks.Open(infile, 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
excelApp.ActiveWorkbook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, outfile);
excelApp.DisplayAlerts = false;
excelApp.SaveWorkspace();
excelApp.Quit();

该部分有效,但有时我的文件超过 20 列,并且输出“分解”为 PDF 上的多个页面。

有什么方法可以控制我想允许多少“休息”?

例如。
假设工作表有 25 列
我输入了一个数字,例如。 3
前 5 列打印在第一页上
接下来的 5 列打印在第 2 页上
接下来的 5 列打印在第 3 页上
并且执行停止(或继续到下一张)

我认为答案在某个地方(但我不太确定):
((Microsoft.Office.Interop.Excel._Worksheet)excelApp.ActiveSheet).PageSetup.Pages

最佳答案

您应该能够通过 PageSetup 类完成此操作并选择适当的 PrintArea。对于 PDF 转换,我们使用“FitToPages”功能来缩放 View ,以便每个工作表仅占一页宽。

_Workbook document = application.Workbooks.Open(FileName,
                     oFalse, oTrue, oMissing, oMissing, oMissing, oMissing,
                     oMissing, oMissing, oFalse, oFalse, oMissing, oFalse,
                     oFalse, oFalse);

foreach (Worksheet ws in document.Worksheets.OfType<Worksheet>())
{
    ws.PageSetup.Orientation = XlPageOrientation.xlLandscape;
    ws.PageSetup.Zoom = false;
    ws.PageSetup.FitToPagesWide = 1;
    ws.PageSetup.FitToPagesTall = false;
}

document.ExportAsFixedFormat(fxFormat, fileName,
                             oMissing, oMissing, oFalse, oMissing,
                             oMissing, oFalse, oMissing);

或者您可以设置打印区域而不是缩放:
    // Specified in the same range format as the UI
    ws.PageSetup.PrintArea = "a1-e50";

您可能需要从 ws 获取使用的列和行。 UsedRange属性以提供正确的打印区域。确保在调用“ExportAsFixedFormat”时为 IgnorePrintAreas 参数提供 false。

关于c# - Excel 到 PDF 导出 - 页码限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12040172/

相关文章:

c# - 如何为 UAP 拍摄媒体元素的当前帧(Windows 10)

javascript - 如何在 AngularJS 中将 JSON 导出或转换为 Excel?

c# - 使用 protobuf-net 生成器制作 PCL 友好的 C# 类

c# - 从文件扩展名获取 ImageFormat

excel - 如何创建一个宏来为范围内的公式添加前缀和后缀?

excel - 来自枢轴的动态图表

excel - 如何强制 Excel 在打开 csv 文件时不删除行?

c# - VSTO PowerPoint/Excel 交互

c# - Raspberry Pi 和单声道帧缓冲区输入

c# - LINQ OrderBy 基于行值