问题描述
我创建了一个简单的 powershell 脚本,它将创建一个 excel 实例并保存一个工作簿:
I created a simple powershell script that will create an excel instance and save a workbook:
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Open("C:\Test\foo.xls")
$workbook.SaveAs("C:\Test\bar.xls")
# cleanup code ...
当我直接从 powershell 运行它时,它工作正常.我创建了一个运行它的计划任务,当我设置了仅在用户登录时运行"的选项时,它就会正常运行.
When I run this from powershell directly it works fine.I created a scheduled task that runs it, and when I have the option set that will "Run only when the user is logged in" then it will run fine.
当我将此选项更改为运行时,无论用户是否登录,尝试打开/保存文件都会失败.我使用的帐户具有正确的权限.我已将帐户设置为作为服务登录.
When I change this option to run whether the user is logged in or not, it will fail trying to open/save the file. The account I am using has the correct permissions. I have the account set up to Log in as a service.
有什么建议吗?
推荐答案
我记得必须在 C# 应用程序中做类似的事情,当你在 Visual Studio 上构建它时运行良好,但在 CI 服务器上的服务下运行失败.我认为这是 Office 自动化本身的局限性,Microsoft 根本不建议/支持这样做,请在此处查看有关 Office 服务器端自动化的注意事项 - http://support.microsoft.com/kb/257757.它显示了问题和替代方案.
I remember having to do something similar in a C# application, which went well when you build it on Visual Studio, but running under a service on the CI server failed. This I believe is the limitation of Office Automation itself and Microsoft doesn't recommend / support doing this at all Look at Considerations for server-side Automation of Office here - http://support.microsoft.com/kb/257757. It shows the problems and the alternatives.
就我而言,我不得不放弃 Office Interop,而使用 EPPlus ( http://epplus.codeplex.com/ ) 使用 excel.它工作得很好,而且速度也快得多.
In my case, I had to give up Office Interop, and use EPPlus ( http://epplus.codeplex.com/ ) to work with excel. It worked great and was much faster as well.
这篇关于Powershell Excel 自动化 - 在计划任务中保存/打开失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!