1

I have a web service method which search string in given Excel file and return its status. This is working okay in VS project. But when I public same web service in IIS its throwing error: I'm working on Windows7 machine

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);

I have checked filePath C:\inetpub\wwwroot\MyFirstWebService\App_Data\Results.xlsx and it is okay.

I also tried with few online solutions like below, But its not fixed yet:

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")

Why it's working on VS not on IIS? my user account got admin right too, wonder why its not working.

Thanks

Ashok
  • 601
  • 1
  • 17
  • 32
  • If it's the same machine, it has to be related to permissions, go to IIS and see which user is running the application pool, than try opening the file with FileInfo in order to see if you can access the file at all – Elisheva Wasserman Jun 21 '15 at 19:22
  • added IIS_IUSRS group permission to Desktop folder, now its working. – Ashok Jun 22 '15 at 16:19

1 Answers1

3

I was having issue while reading the Excel from MVC application hosted on IIS. Two things are important for accessing the Excel from IIS.

  1. Must have "C:\Windows\SysWOW64\config\systemprofile\Desktop". i.e. Desktop folder in SysWOW64 folder, System32 folder for 32 bit.
  2. Assign full access to IIS_IUSRS user on Desktop folder.

It`s kind of weired, not sure why this is required but worked for me.

H_Dev
  • 31
  • 5
  • This worked for me in addition of https://stackoverflow.com/questions/17785063/retrieving-the-com-class-factory-for-component-error-80070005-access-is-de => Set the Identity (Model Name) of application pool to LocalSystem. – Flou Oct 14 '22 at 16:24