【问题标题】:C# + Excel - How do I fix "Value not within expected range" error in a call to Worksheet.ExportAsFixedFormat()?C# + Excel - 如何在调用 Worksheet.ExportAsFixedFormat() 时修复“值不在预期范围内”错误?
【发布时间】:2018-05-16 09:58:26
【问题描述】:

我正在尝试将 Excel 工作簿中的每张工作表打印为单独的 PDF。但我不断收到System.ArgumentException: 'Value does not fall within the expected range.' 在线w.ExportAsFixedFormat(...);

实现此目的的正确语法是什么?

我使用的代码:

foreach (Worksheet w in ws.Worksheets)
{
     var wname = Path.Combine(
         AppDomain.CurrentDomain.BaseDirectory,
         "Export\\" + w.Name + ".pdf"
     );

     w.ExportAsFixedFormat(
         XlFixedFormatType.xlTypePDF,
         wname,
         XlFixedFormatQuality.xlQualityStandard,
         true,
         false,
         1,
         w.PageSetup.Pages.Count,
         false,
         false
     );
}

更新: 我应该提一下,我将它与 Office 2013、Microsoft Excel 15.0 对象库一起使用

【问题讨论】:

标签: c# excel office-interop export-to-pdf


【解决方案1】:

查看方法的文档,您的问题可能是最后一个参数。 Workbook.ExportAsFixedFormat Method

试试:

foreach (Worksheet w in ws.Worksheets)
{
     var wname = Path.Combine(
         AppDomain.CurrentDomain.BaseDirectory,
         "Export\\" + w.Name + ".pdf"
     );

     w.ExportAsFixedFormat(
         XlFixedFormatType.xlTypePDF,
         wname,
         XlFixedFormatQuality.xlQualityStandard,
         true,
         false,
         1,
         w.PageSetup.Pages.Count,
         false
     );
}

(从方法调用中删除了最后一个参数) 让我知道这是否有效:)

【讨论】:

  • 工作就像一个魅力!谢谢!
猜你喜欢
  • 2010-12-02
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多