简体   繁体   English

Windows 服务中的 Excel OLE 自动化

[英]Excel OLE automation in a Windows service

I have a Windows service filling cells in an Excel Workbook via OLE Automation.我有一个 Windows 服务通过 OLE 自动化填充 Excel 工作簿中的单元格。

The save process is not working, the file is never saved.保存过程不起作用,文件从未保存。 But when done in a GUI app, the save process works.但是当在 GUI 应用程序中完成时,保存过程会起作用。

The service user is an administrator account.服务用户是管理员帐户。 I suspect the interaction with the Desktop.我怀疑与桌面的交互。

Any idea?任何的想法?

Don't do this.不要这样做。 Find another solution.寻找另一个解决方案。 Microsoft does not support or recommend OLE automation of Office software from anywhere but a workstation desktop session. Microsoft 不支持或建议从工作站桌面会话以外的任何地方实现 Office 软件的 OLE 自动化。 I can't paraphrase any better than what they say directly, so here's the advice (link above goes into more detail) :我无法解释比他们直接说的更好,所以这是建议(上面的链接更详细):

All current versions of Microsoft Office were designed, tested, and configured to run as end-user products on a client workstation. Microsoft Office 的所有当前版本都经过设计、测试和配置,以在客户端工作站上作为最终用户产品运行。 They assume an interactive desktop and user profile.他们假设一个交互式桌面和用户配置文件。 They do not provide the level of reentrancy or security that is necessary to meet the needs of server-side components that are designed to run unattended.它们不提供满足设计为无人值守运行的服务器端组件的需要所必需的重入或安全级别。

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 时出现死锁。

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution.如果您正在构建一个在服务器端上下文中运行的解决方案,您应该尝试使用已为无人值守执行安全的组件。 Or, you should try to find alternatives that allow at least part of the code to run client-side.或者,您应该尝试找到至少允许部分代码在客户端运行的替代方案。 If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully.如果您使用服务器端解决方案中的 Office 应用程序,该应用程序将缺乏许多成功运行所需的功能。 Additionally, you will be taking risks with the stability of your overall solution.此外,您将面临整体解决方案稳定性的风险。

If you need your service to store data, a database is probably a good choice.如果您需要服务来存储数据,数据库可能是一个不错的选择。 Any users that require the data in an excel spreadsheet could populate their documents from the database.任何需要 Excel 电子表格中的数据的用户都可以从数据库中填充他们的文档。 You could even set up template documents to do this automatically.您甚至可以设置模板文档来自动执行此操作。

I had the same problem, run a Delphi software as service, with an administrator user and it gave to me a lot of errors in the read/write process like SaveAs method of Workbook class failed我遇到了同样的问题,以管理员用户身份运行 Delphi 软件作为服务,它在读/写过程中给了我很多错误,例如Workbook 类的 SaveAs 方法失败

The solution for read/save was create both folders below:读取/保存的解决方案是在下面创建两个文件夹:

C:\\Windows\\System32\\config\\systemprofile C:\\Windows\\System32\\config\\systemprofile
C:\\Windows\\SysWOW64\\config\\systemprofile\\ C:\\Windows\\SysWOW64\\config\\systemprofile\\

There isn't much sense at all, but it worked.根本没有多大意义,但它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM