【问题标题】:Microsoft Office Interop Excel ExportAsFixedFormat - very slow, images not loadedMicrosoft Office 互操作 Excel ExportAsFixedFormat - 非常慢,未加载图像
【发布时间】:2017-01-10 05:57:23
【问题描述】:

我想将生成的 .xlsx 电子表格导出为 PDF。 我有两个问题:

  • 两种方法的导出文档都非常慢 - SaveAs() ExportAsFixedFormat()
  • ExportAsFixedFormat 不导出图像。

我正在使用 IIS 在 ASP.NET 服务器中运行代码。

已经像这里所说的那样配置了权限:https://support.comodo.com/index.php?/Knowledgebase/Article/View/1129/66/access-denied-exception-from-hresult-0x80070005-e_accessdenied

此存储库中的代码可以正常运行:https://github.com/aardvarkss/ExcelPDFExport

我还使用 EPPlus 生成 .xlsx 文件。

代码:

   Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
   Microsoft.Office.Interop.Excel.Workbooks excelWorkbooks = app.Workbooks;
   Microsoft.Office.Interop.Excel.Workbook wkb = excelWorkbooks.Open(this.tempExcelFilePath);

   foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in wkb.Sheets)
   {
        sheet.SaveAs(this.tempPdfFilePath); // Saves the PDF correct AS I want But it cannot finish the task (waiting around 3 mins)
        sheet.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, this.tempPdfFilePath); // Export PDF without image (image currently cannot be displayed), Also slow
   }

   wkb.SaveAs(this.tempPdfFilePath); // Waiting too long and cannot be finished
   wkb.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, "C:\\Users\\user222\\Desktop\\Sample.pdf"); // Waiting too long and cannot be finish the task

   // Closes the EXCEL.exe process in windows. If it not closes it cause errors. 
   wkb.Close(SaveChanges: false);
   excelWorkbooks.Close();
   app.Quit();

   System.Runtime.InteropServices.Marshal.ReleaseComObject(wkb);
   System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbooks);
   System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

任何想法为什么它这么慢?以及如何解决我的问题?

今天开始加载缓慢。在此之前,它不存在加载缓慢的问题。

【问题讨论】:

  • Interop 既不设计也不推荐用于服务器应用程序。如果没有孤立的 EXCEL.EXE,请检查 TaskManager。
  • 在到达 `new Microsoft.Office.Interop.Excel.Application();` 行之前调试时,任务管理器中没有 EXCEL.EXE 进程。
  • 好吧,我想它应该是 Excel 或源文件的东西,因为它在今天之前可以工作。尝试重新启动服务器,看看是否会有所不同。
  • 已经试过了。我仍在本地主机上,重新启动的机器,IIS 等。我在这里仍然出现故障,所以我开始使用iTextSharp - 逐行。

标签: c# asp.net excel interop ms-office


【解决方案1】:

我正在使用 IIS 在 ASP.NET 服务器中运行代码。

Microsoft 目前不推荐也不支持任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)的 Microsoft Office 应用程序自动化,因为 Office 可能表现出不稳定Office 在此环境中运行时的行为和/或死锁。

如果您要构建在服务器端上下文中运行的解决方案,您应该尝试使用已确保无人值守执行安全的组件。或者,您应该尝试找到允许至少部分代码在客户端运行的替代方案。如果您使用服务器端解决方案中的 Office 应用程序,该应用程序将缺少许多成功运行所需的功能。此外,您将承担整体解决方案稳定性的风险。在Considerations for server-side Automation of Office 文章中阅读更多相关信息。

我建议改用 Open XML SDK,请参阅 Welcome to the Open XML SDK 2.5 for Office。您也可以考虑使用为服务器端执行而设计的第三方组件。

【讨论】:

  • 感谢您的回复。我会同意你的。但是使用 Open XML SDK 2.5 for Office 对我来说并不复杂。我将使用iTextSharp 作为替代。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
相关资源
最近更新 更多