Cannot open Excel file in C #

I have the following C # function in my project, which should open and return an existing Excel workbook object:

Application _excelApp; // ... private Workbook OpenXL(string path, string filename) { try { if (_excelApp == null) { _excelApp = new Application(); } Workbook workBook = _excelApp.Workbooks.Open(path + filename, // Name 0, // Do not update links true); // Open read-only return workBook; } catch (Exception e) { _excelApp = null; throw new ArgumentException("Error opening " + path + filename, e); } } 

But when I run it with "C: \" and "scratch.xlsx", calling Open () causes the following error:

 Microsoft Excel cannot access the file 'C:\scratch.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. 

The file and path exist: I copied the path from the error message and pasted it into the command window, and the file was uploaded to Excel. The file is not locked: Excel can open it, but my program cannot, even immediately after a reboot. I am not trying to save it, I am trying to open it, so the last option does not matter.

I do not understand why this simple piece of code does not work. Any suggestions would be greatly appreciated.

[edit] Now I tried to open this file from my personal network drive (M :) and from a USB drive. All to no avail.

The application is actually a Windows service running under the local system account and generating reports. He is currently writing CSV reports without any access issues. Now I am trying to open an excel file as a template report and fill in various fields. This is when an Excel file opens, which it fails. I think that the administrator account option that everyone offers is a red herring, as it can write CSV files without any problems. [/ Edit]

--- Alistair.

+8
c # excel-interop
source share
3 answers

I found the following page:

http://social.msdn.microsoft.com/Forums/en-US/b81a3c4e-62db-488b-af06-44421818ef91/excel-2007-automation-on-top-of-a-windows-server-2008-x64

Where it says that ...

It is not supported for automating the user interface of office products. It seems that Windows Server 2008 and Excel 2007 use this operator.

The user then asks in which situation I encountered Windows Service, which cannot open the Excel file, although the same code on the command line has no problems.

In response, the creation of the following folder is reported:

Windows 2008 Server x64: C: \ Windows \ SysWOW64 \ config \ systemprofile \ Desktop

Windows 2008 Server x86: C: \ Windows \ System32 \ config \ systemprofile \ Desktop

I tried this and it worked! Can someone explain why this is necessary and some shortcomings?

Thanks,

--- Alistair.

+31
source share

Run the program as admin, C: / cannot be accessed if the user does not work as admin. You can force your program to request the user to run as an administrator by changing ApplicationManifest: How to make a .NET application run as an administrator?

+3
source share

I ran into the same problem and I researched the registry root information.

In the end, I found another solution that does not change the registry values ​​and everything works correctly.

This decision ...

Windows 2008 Server x64

Please create this folder.

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

Windows 2008 Server x86

Please create this folder.

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

... instead of dcomcnfg.exe.

This operation fixed the problems with office automation in my system.

The desktop folder seems necessary in the systemprofile folder to open the Excel file.

It disappears with Windows2008, Windows2003 has a folder, and I think this is causing this error.

I think this is safer than hacking the registry.

If you try this solution, let me know the results.

+2
source share

All Articles