2017-01-09 100 views
0

我想产生的.xlsx电子表格导出为PDF。 我有两个问题:的Microsoft Office Excel的互操作ExportAsFixedFormat - 很慢,图像不加载

  • 导出文件是两个方法非常缓慢 - 另存为()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); 

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

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

+0

互操作既不旨在也不建议在服务器应用中使用。检查TaskManager是否没有孤立的EXCEL.EXE。 – Alex

+0

在到达新的Microsoft.Office.Interop.Excel.Application();行之前进行调试时,在任务管理器中没有EXCEL.EXE进程。 – mihkov

+0

好吧,我想它应该是Excel或源文件,因为它在今天之前工作。尝试重新安装服务器,看看它是否会有所作为。 – Alex

回答

1

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

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

如果您要构建在服务器端上下文中运行的解决方案,你应该尝试使用已取得安全的无人参与的执行部件。或者,您应该尝试找到允许至少部分代码运行客户端的替代方案。如果您从服务器端解决方案使用Office应用程序,则该应用程序将缺少成功运行所需的许多必要功能。此外,您将面临整体解决方案稳定性的风险。请阅读Considerations for server-side Automation of Office文章中的更多内容。

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

+0

感谢您的回复。我会同意你的意见。但使用Open XML SDK 2.5 for Office对我而言并不复杂。我将使用'iTextSharp'作为替代。 – mihkov

相关问题