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

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

  • 导出文档对于这两种方法都很慢–SaveAs()ExportAsFixedFormat()
  • ExportAsFixedFormat不会导出图像。

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

已经configuration的权限就像这里所说: 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); 

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

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

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

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

如果您正在构build一个在服务器端上下文中运行的解决scheme,则应该尝试使用已经安全的组件进行无人值守的执行。 或者,您应该尝试find允许至less部分代码运行在客户端的备选scheme。 如果从服务器端解决scheme使用Office应用程序,那么应用程序将缺less许多必要的function来成功运行。 此外,您将面临整体解决scheme稳定性的风险。 请阅读“服务器端Office自动化的注意事项”文章中的更多内容。

我build议使用Open XML SDK,请参阅欢迎使用Open XML SDK 2.5 for Office 。 您也可以考虑使用专为服务器端执行而devise的第三方组件。