Microsoft Office Excel无法访问IIS上的文件

问题描述 投票:0回答:1

我有一个Web服务方法,该方法在给定的Excel文件中搜索字符串并返回其状态。在VS项目中这可以正常工作。但是,当我在IIS中公开相同的Web服务时,会引发错误:我正在Windows7计算机上工作]

System.Runtime.InteropServices.COMException: Microsoft Office Excel cannot access the file 'C:\inetpub\wwwroot\MyFirstWebService\App_Data\Results.xlsx'. 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.


string filePath = Server.MapPath("~") + @"\App_Data\Results.xlsx";
            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(filePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

我已经检查了filePath C:\inetpub\wwwroot\MyFirstWebService\App_Data\Results.xlsx,没关系。

我也尝试过以下几种在线解决方案,但尚未解决:

Create directory "C:\Windows\SysWOW64\config\systemprofile\Desktop " (for 64 bit Windows) or 
"C:\Windows\System32\config\systemprofile\Desktop " (for 32 bit Windows)

Set Full control permissions for directory Desktop (for example in Win7 & IIS 7 & DefaultAppPool set permissions for user 
    "IIS AppPool\DefaultAppPool")

为什么它在VS上不能在IIS上运行?我的用户帐户也获得了管理员权限,想知道为什么它不起作用。

谢谢

c# asp.net excel web-services iis
1个回答
0
投票

我从IIS上托管的MVC应用程序读取Excel时遇到问题。对于从IIS访问Excel,有两点很重要。

  1. 必须具有“ C:\ Windows \ SysWOW64 \ config \ systemprofile \ Desktop”。即SysWOW64文件夹中的Desktop文件夹,32位的System32文件夹。
  2. 为桌面文件夹上的IIS_IUSRS用户分配完全访问权限。

有点奇怪,不确定为什么要这样做但是对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.