【发布时间】: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