Excel Windows 服务中的 OLE 自动化

Excel OLE automation in a Windows service

我有一个 Windows 服务通过 OLE 自动化填充 Excel 工作簿中的单元格。

保存过程不工作,文件永远不会保存。但是在 GUI 应用程序中完成时,保存过程有效。

服务用户是管理员帐号。我怀疑与桌面的交互。

有什么想法吗?

不要这样做。寻找其他解决方案。 Microsoft does not support or recommend 从工作站桌面会话以外的任何地方对 Office 软件进行 OLE 自动化。我无法解释比他们直接说的更好,所以这里是建议(link 上面有更多细节):

All current versions of Microsoft Office were designed, tested, and configured to run as end-user products on a client workstation. 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.

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. Additionally, you will be taking risks with the stability of your overall solution.

如果您的服务需要存储数据,数据库可能是一个不错的选择。任何需要 excel 电子表格中的数据的用户都可以从数据库中填充他们的文档。您甚至可以设置模板文档来自动执行此操作。

我遇到了同样的问题,运行 一个 Delphi 软件即服务,管理员用户,它在 read/write 过程中给了我很多错误,比如 工作簿的另存为方法class失败

read/save 的解决方案是创建以下两个文件夹:

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

一点意义都没有,但它起作用了。