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

C#ExcelExport to-ExcelExcel Interop

C# Problem Overview


I have my code as follows :-

Microsoft.Office.Interop.Excel.Application oXL = null;
Microsoft.Office.Interop.Excel.Sheets sheets;
Application excel = new Microsoft.Office.Interop.Excel.Application();


excel.Workbooks.Add(System.Reflection.Missing.Value);

/*
    * Here is the complete detail's about Workbook.Open()
    * 
    *  Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, 
    *  Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin,
    *  Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)
    */

Workbook workbook = excel.Workbooks.Open(
    System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Template.xlsx"),
    Missing.Value, true, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value,
    Missing.Value);
sheets = workbook.Worksheets;

Now for the line :-

workbook = excel.Workbooks.Open(
    System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Template.xlsx"),
    Missing.Value, true, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value,
    Missing.Value);

It gets executed directly from the visual studio (F5) but when i try to access it with IIS it wont work. Throws error as follows:-

> Microsoft Office Excel cannot access the file 'c:\inetpub\wwwroot\Timesheet\App_Data\Template.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.

I have tried the work around as:-

> - Folder and the file exist, giving access to the IUSR_### (IIS user) and to the ASPNET user in the folder where the file is.

  • At Component Services(DCOM) given access to appropriate user.

I have already given all permission's to the folder where the template(.xlsx) exists

Any suggestions??

C# Solutions


Solution 1 - C#

Try this:

  1. Create the directory

> C:\Windows\SysWOW64\config\systemprofile\Desktop

(for the 32-bit version of Excel/Office on a 64-bit Windows computer) or

> C:\Windows\System32\config\systemprofile\Desktop

(for a 32-bit version of Office on a 32-bit Windows computer or a 64-bit version of Office on a 64-bit Windows computer).

  1. For the Desktop directory, add Full control permissions for the relevant user (for example in Win7 & IIS 7 & DefaultAppPool set permissions for user IIS AppPool\DefaultAppPool).

Original post with answer:

Solution 2 - C#

Let me note that in my place, adding the c:\windows\syswow64\config\systemprofile\desktop directory didn't work.

The point is that WOW64 stands for Windows on Windows64, that means it actually applies for 32-bit programs running on the 64bit OS.

Since I have 64-bit Excel installed, the proper directory turned out to be the c:\windows\system32\config\systemprofile\desktop

Solution 3 - C#

In my case, I followed the suggestions provided here and solved the problem.

Steps:

  1. Run dcomcnfg
  2. Go to Console Root \Component Services\Computers\My Computer\DCOM Config\Microsoft Excel Application
  3. Right click Microsoft Excel Application
  4. Select Properties
  5. Go to Identity tab
  6. Select The interactive user.

In step 2, if you can't locate that path then try running mmc comexp.msc /32 instead of dcomcnfg.

Solution 4 - C#

I would like to add something to the Eric Bonnot's answer : The answer worked out partially as I worked on a IIS server with a non-admin user with the powerpoint PIA.

I noticed that I could not open a pptx file if this one had a media (picture for example) in it.

The "hack" was to add also the rights to the windows user (the one using the PIA) on the systemprofile/AppData directories.

Hope this helps

Solution 5 - C#

I was previously attempt this problem then i resolved.

Solution:

I put the full permission to particular folder(Sub folder and files) and checked working fine.

Solution 6 - C#

I've wrapped my WCF in a Windows Service. Creating the Desktop Folders did solve it for me on one machine, but not on another.

My problem in the end was, that my Windows Service did not run under an active User of the machine. Configuring the service to run under a user which is active on the machine solved this problem for me so far.

Only the combination of

  • Existing Desktop Folder
  • Service running under a real user Account

got it working for me.

This article lead me to the full solution: https://stackoverflow.com/questions/14037412/cannot-access-excel-file

Solution 7 - C#

This works

> excel.exe /safe

This does not and gives the same error as the regular excel startup

> excel.exe /automation

This also occurs for all MS Office 2007 apps for ANY network file. Local file access is fine.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShubhView Question on Stackoverflow
Solution 1 - C#Eric BonnotView Answer on Stackoverflow
Solution 2 - C#NikhilView Answer on Stackoverflow
Solution 3 - C#datchungView Answer on Stackoverflow
Solution 4 - C#Amaury BauzacView Answer on Stackoverflow
Solution 5 - C#user2617875View Answer on Stackoverflow
Solution 6 - C#Florian MoserView Answer on Stackoverflow
Solution 7 - C#wbazView Answer on Stackoverflow