ASP.NET WebApi无法在Azure中使用COM(Excel)

COM对象有一些问题

我有一个Web API控制器,创build和保存Excel文件。

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); excel.Workbooks.Add(); Microsoft.Office.Interop.Excel._Worksheet workSheet = excel.ActiveSheet; try { workSheet.Cells[1, "A"] = "Id"; -//- int row = 2; foreach (var _c in db.Clients) { workSheet.Cells[row, "A"] = _c.ClientId; -//- row++; } workSheet.Range["A1"].AutoFormat(Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic1); fileName = string.Format(@"{0}/ExcelData.xlsx", System.Web.HttpContext.Current.Server.MapPath("~/Reports")); workSheet.SaveAs(fileName); 

在创build之后,控制器为这个文件返回一个url。 在我的本地主机上,它工作得非常好,但作为主机部署到Azure云后,我有一个例外:

ExceptionMessage:

 "Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))." ExceptionType: "System.Runtime.InteropServices.COMException" Message: "An error has occurred." StackTrace: " at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) 

等等。

我已经尝试在x64和x86之间切换 – 它没有帮助

如果有人知道如何解决这个问题,告诉我

我有和你一样的问题。 我发现了微软的官方声明:

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

您将看到详细信息: http : //support.microsoft.com/kb/257757

我在博客中find了这个解决scheme

https://debugmode.net/2011/08/28/creating-and-updating-excel-file-in-windows-azure-web-role-using-open-xml-sdk/