使用 ASP.NET 自动化 Excel
背景
我们正在使用 ASP.NET 2.0 开发一些内部实用程序。 其中之一是从数据库中提取一些信息,并构建一个 Excel 工作簿,其中包含许多电子表格,其中包含基于数据库查询的数据。
问题
概念验证原型(一个简单的 ASP.NET 页面,从数据库查询单个项目并打开 Excel 以将数据添加到工作表)在开发计算机上本地运行时运行良好,可以愉快地创建和显示 Excel按要求制作电子表格。 但是,当在我们的服务器上运行时,我们在尝试实例化 Excel 时收到以下错误。
无法将类型“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Excel._Application”。 此操作失败,因为对 IID 为“{000208D5-0000-0000-C000-000000000046}”的接口的 COM 组件上的 QueryInterface 调用由于以下错误而失败:不支持此类接口(HRESULT 异常:0x80004002 (E_NOINTERFACE)) 。
解决方案?
我们正在使用 Excel 2003 的 PIA,并且我们在服务器上安装了 Excel 2003 和 PIA。 谁能解释为什么这不起作用,或者给我们一些关于如何追踪问题的提示?
感谢您提供的任何帮助。
Background
We are developing some in-house utilities using ASP.NET 2.0. One of which is extracting some information from databases and building an Excel workbook containing a number of spreadsheets with data based on queries into the database.
Problem
The proof-of-concept prototype (a simple ASP.NET page that queries a single item from the database and opens Excel to add data to a worksheet) is working well when run locally on the development machines, happily creating and displaying an Excel spreadsheet as requested. However, when run on our server, we get the following error upon trying to instantiate Excel .
Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Solution?
We are using the PIA for Excel 2003 and we have Excel 2003 and the PIA installed on the server. Can anyone explain why this isn't working or give us some tips on how we might track the problem down?
Thanks for any assistance you can provide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
运行 ASP.NET 应用程序池的用户是否可以访问该应用程序? 尝试以该用户身份登录(或将应用程序池更改为以该用户身份运行)并打开 Excel。 如果可行,请尝试使用失败的代码以该用户身份在服务器上运行 WinForms 应用程序。
不确定,但我认为 PIA 程序集可能需要通过 regsvr32 注册。
我怀疑如果您作为网络服务运行,您将无法启动 Excel(无交互式登录、受限帐户等)。 您的 ASP.NET 代码在应用程序池内运行。 您可以通过 IIS 管理器更改应用程序池运行的用户。 如果您想检查当前正在运行的代码,请在任务管理器中查找 w3wp 进程。
为了进行测试,请将应用程序池更改为以您认识的使用 Excel 的用户的身份运行。
Can the user that the ASP.NET Application Pool runs under have access to the application? Try logging in as that user (or change the Application Pool to run as that user) and opening Excel. If that works try running a WinForms application on the server as that user with the code that is failing.
Not sure but I think the PIA assemblies might need to be registered via regsvr32.
I suspect that if you run as Network Service, you will not be able to start Excel (no interactive login, restricted account, etc). Your ASP.NET code runs inside the application pool. You can change the user the application pool runs as through the IIS manager. If you want to check what your code is currently running as look for the w3wp process in Task Manager.
For testing, change the Application Pool to run as the user you know works with Excel.
我们使用 Aspose(商业)。 服务器上的办公室没什么乐趣。
它被称为 PI(t)A 是有原因的......
We use Aspose (commercial). Office on a server is not much fun.
It is called PI(t)A for a reason...
考虑使用 XLSX 文件(Office 2007 中的新功能,但 Office 2003 中有一个插件),这些文件只是包含 XML 文件的 ZIP 文件,无需 Excel 即可操作。 (基于 XML 的)SpreadsheetML 有很好的文档记录,并且编程起来也不是太复杂(您甚至可能在网络上的某个地方找到 LINQ to SpreadsheetML)。
正如上面指出的,Excel 并不是真正的服务器产品,在服务器上使用它时可能会遇到各种问题。
Consider working with XLSX files (new in Office 2007, but there is a plugin for Office 2003), which are just ZIP files containing XML files which you can manipulate without the need for Excel. The (XML-based) SpreadsheetML is well documented and not too complicated to program against (you might even find a LINQ to SpreadsheetML somewhere on the web).
As was pointed out above, Excel is not really a server product and you might run into all kinds of issues when using it on a server.
我认为问题在于,一旦将应用程序部署到 IIS,您就会突然在 MTA COM 公寓内运行。 我认为 Excel 是一个 STA 组件,因此不能在 MTA 内创建。 您需要在您使用的页面中设置 aspcompat 选项
更多信息< /a>
I think the problem is that once you deploy your application to IIS you are suddenly running inside an MTA COM Apartment. I believe that Excel is an STA component and therefore cannot be created inside the MTA. You will need to set the aspcompat option in the page you are using
More Info Here
来自 Microsoft(强调原始来源):
列出了不应执行此操作的原因:
并且您的代码可能会引发以下错误:
CoCreateInstance
最后:
From Microsoft, (emphasis in original source):
With a list of reasons why you shouldn't do it:
And your code might throw the following errors:
CoCreateInstance
And finally: