【问题标题】:Microsoft Office Excel cannot access the file on IISMicrosoft Office Excel 无法访问 IIS 上的文件
【发布时间】:2015-06-21 13:41:21
【问题描述】:

我有一个 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 上运行?我的用户帐户也获得了管理员权限,不知道为什么它不起作用。

谢谢

【问题讨论】:

  • 如果是同一台机器,那得和权限有关,去IIS看看是哪个用户在运行应用程序池,然后用FileInfo打开文件看看能不能访问文件
  • 向桌面文件夹添加了 IIS_IUSRS 组权限,现在可以使用了。

标签: c# asp.net excel web-services iis


【解决方案1】:

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

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

这有点奇怪,不知道为什么这是必需的,但对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    相关资源
    最近更新 更多