1

Using C# and ASP.NET, I'm trying to read an Excel file on server via Microsoft Interop, apply some processing to it, save it then download it on client machine. On local host, it works fine but on client machine it displays this message

Server Error in '/' Application.

Microsoft Office Excel cannot access the file 'C:\inetpub\wwwroot\ICNMS\Mysheet.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.

Here is my code:

    Microsoft.Office.Interop.Excel.Application objApp;
    Microsoft.Office.Interop.Excel.Workbook workBook;

    Microsoft.Office.Interop.Excel.Sheets objSheets;
    Microsoft.Office.Interop.Excel._Worksheet workSheet;

    Microsoft.Office.Interop.Excel.Range range;

    objApp = new Microsoft.Office.Interop.Excel.Application();
    workBook = objApp.Workbooks.Open("Mysheet.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);

The Excel file's path doesn't exceed 218 characters and isn't opened or used by any other application.By the way, the file exists in the same directory as the web page's. Any help would be very much appreicated.Thanks in advance.

Doua Ali
  • 175
  • 1
  • 5
  • 21
  • 3
    Does the application pool user have sufficient privileges to open the document? My advice: don't put the document in the same folder as your web application, and make sure the application pool user has 'Modify' privileges on the folder the excel file is in. – Stephen Feb 18 '15 at 08:25
  • Yes, the comment from Stephen was the first I thought too. And also check authentication in IIS: anonymous uses App pool user, whereas Windows auth uses the client user (browsing the page). Check for Modify / Change ! – Rainer Schaack Feb 18 '15 at 08:56
  • Thanks guys for your concern. I tried to add the app pool to the file's permitted users but it showed the error:The following object is not from a domain listed in the Select Location dialog box, and is therefore not valid. Any hints, please? – Doua Ali Feb 18 '15 at 11:24

1 Answers1

1

Thanks everyone. The problem was solved; I followed the steps provided in the answer to this question:

Microsoft Office Excel cannot access the file 'c:\inetpub\wwwroot\Timesheet\App_Data\Template.xlsx'

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

Community
  • 1
  • 1
Doua Ali
  • 175
  • 1
  • 5
  • 21