C# 打开 excel 错误:System.Runtime.InteropServices.COMException (0x80080005): Retrieving the COM class factory for component with CLS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14716047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
opening excel error: System.Runtime.InteropServices.COMException (0x80080005): Retrieving the COM class factory for component with CLSID
提问by user2044518
I have a C# .net web app. I want a user to click a button on a webpage, the server to open an excel spreadsheet and write data to it, and the user to save the document. This works locally using Visual Studio 2010 and Office 2010. On my server, however, I get this error:
我有一个 C# .net 网络应用程序。我希望用户单击网页上的按钮,服务器打开 Excel 电子表格并向其中写入数据,以及用户保存文档。这在本地使用 Visual Studio 2010 和 Office 2010 工作。但是,在我的服务器上,我收到此错误:
System.Runtime.InteropServices.COMException (0x80080005): Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
System.Runtime.InteropServices.COMException (0x80080005):检索 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件的 COM 类工厂由于以下错误而失败:80080005 Serverx0RES_EVER_EX0SER_8000E_EXER_800800000E_EXER_8008000050 ))。
I have researched and found a ton of answers stating to open dcomconfig and add the user with permissions to Microsoft Excel Application. My site is set to passthrough security so I added myself; I am an admin on the server, so I ensured Admins had access; I also added the App Pool default of Network Service; also ASP.NET Machine Account; and, just in case, added the account the app uses when connecting to the database. I still get the exact same error.
我研究并发现了大量答案,说明打开 dcomconfig 并将具有权限的用户添加到 Microsoft Excel 应用程序。我的网站设置为直通安全,所以我添加了自己;我是服务器的管理员,所以我确保管理员可以访问;我还添加了网络服务的应用程序池默认值;还有 ASP.NET 机器帐户;并且为了以防万一,添加了应用程序在连接到数据库时使用的帐户。我仍然得到完全相同的错误。
My server is 64 bit, Office 2007, IIS 7.
我的服务器是 64 位,Office 2007,IIS 7。
My code is:
我的代码是:
{
try
{
Excel.Application xXL = null;
Excel.Workbook xWB = null;
Excel.Worksheet xSheet = null;
xXL = new Excel.Application();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
回答by Avner Shahar-Kashtan
Office automation - the usage of these Excel.Application and Excel.Workbook objects - is not recommended and not supported by Microsoft. Look:
办公自动化 - 这些 Excel.Application 和 Excel.Workbook 对象的使用 - 不推荐且Microsoft 不支持。看:
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
Microsoft 当前不建议也不支持从任何无人参与的非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)自动化 Microsoft Office 应用程序,因为 Office 可能表现出不稳定的行为和/或在此环境中运行 Office 时出现死锁。
This means that even if yo sort out your server-side/client-side issues, you're still playing with fire, since some scenarios might work, others won't, and others will sometimes work and sometimes cause your process to hang because Excel attempts to open a pop-up notification when there isn't a console user logged onto the server. Trust me, don't do it.
这意味着即使你解决了你的服务器端/客户端问题,你仍然在玩火,因为有些场景可能会起作用,有些则不会,而其他的有时会起作用,有时会导致你的进程挂起,因为当没有控制台用户登录到服务器时,Excel 会尝试打开弹出通知。相信我,不要这样做。
The link I provided lists several alternatives to what you're trying to do, but assuming you're planning on building relatively simple Excel sheets for your user, you're better off just creating a CSV fileand sending that back to the user with the appropriate header (something like Response.ContentType="application/vnd.ms-excel"
) so that the returned data will be opened by Excel by default.
You can see more answers to that question here.
我提供的链接列出了您尝试执行的操作的几种替代方法,但假设您计划为您的用户构建相对简单的 Excel 工作表,您最好只创建一个CSV 文件并将其发送回用户适当的标题(类似于Response.ContentType="application/vnd.ms-excel"
),以便默认情况下 Excel 将打开返回的数据。您可以在此处查看该问题的更多答案。
回答by Viral Prajapati
- In DCOMCNFG, right click on the My Computer and select properties.
- Go to Component /MyComputer/DCOMConfig
- Go to Microsoft Excel Application property Security
- In launch and Activation Permissions, click "Custamize" and add Network Service to it and give it "Local launch" and "Local Activation" permission. give same As Acess Permissions and cofiguration Permissions Press OK and thats it. i can run my application now.
- 在 DCOMCNFG 中,右键单击我的电脑并选择属性。
- 转到组件 /MyComputer/DCOMConfig
- 转到 Microsoft Excel 应用程序属性安全性
- 在启动和激活权限中,单击“自定义”并向其添加网络服务并为其授予“本地启动”和“本地激活”权限。给予相同的访问权限和配置权限按确定,就是这样。我现在可以运行我的应用程序了。