为什么我在运行Windows服务时收到错误Microsoft Excel无法访问该文件? [英] Why I am getting the error Microsoft Excel cannot access the file while running a windows service?

查看:143
本文介绍了为什么我在运行Windows服务时收到错误Microsoft Excel无法访问该文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在远程计算机上运行Windows服务时出现以下错误。

操作系统是Windows 7(64位)。有权限。

路径是正确的。该文件也未打开。打开工作簿时出错。



I am getting following error while running a windows service in a remote machine.
The OS is Windows 7 (64 bit). Permission is there.
The path is correct. The file is not open either. Error occuring on opening the workbook.

Microsoft Office Excel cannot access the file '\\ntp-228\SMART_PDF_FILE_STORE\TEMPLATE\T_001.xls'. There are several possible reasons:

• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.





我的代码如下:



My code is as below:

_Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                          xlApp.DisplayAlerts = false;
                          //fetch the Quotation No after Splitting pdf data
                          object misValue = System.Reflection.Missing.Value;
                          _Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
                          //read the Standard Excel Template fILE(Path Included)(Renamed) set in the Template Folder configured in App.Config
                          xlWorkBook = xlApp.Workbooks.Open(ExcelTemplateFullPath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                          _Worksheet xlWorkSheet = (_Worksheet)xlWorkBook.Worksheets.get_Item(1);

推荐答案

好的,首先,Microsoft不支持并积极阻止在非用户应用程序中使用Office Interop,即:服务和Web应用程序。



Office不是为支持非交互式环境而编写的。您可能在服务中使用Office Interop时遇到问题。



其次,您正尝试在网络共享上打开文件。如果您的服务运行的帐户没有该共享的权限,您将无法打开该文件。在本地系统下运行您的服务真的很好,根本无法访问任何网络共享。



解决方案是创建有权访问该共享的用户帐户,为该帐户授予LogonAsService权限,并在该帐户下启动您的服务。
OK, first and foremost, Microsoft does not support and actively discourages using Office Interop in non-user applications, i.e.: Services and web applications.

Office was not written to support a non-interactive environment. You can have problems using Office Interop in your service.

Secondly, you're trying to open a file on a network share. If the account your service is running under does not have permissions to that share you're not going to be able to open the file. Chances are really good that you're running your service under Local System, which will not have access to any network shares at all.

The solution to that is to create a user account that has access to that share, give the account the LogonAsService permission and start your service under that account instead.


这篇关于为什么我在运行Windows服务时收到错误Microsoft Excel无法访问该文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆