Microsoft Office Interop Excel ExportAsFixedFormat - 非常慢,图像未加载

Microsoft Office Interop Excel ExportAsFixedFormat - very slow, images not loaded

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

我是 运行 ASP.NET 服务器中带有 IIS 的代码。

已经像这里说的那样配置了权限: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);

知道为什么这么慢吗?以及如何解决我的问题?

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

I am running the code in ASP.NET server with IIS.

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

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

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