C# "cannot access file" opening Excel file with Excel.Interop
I inherited an older C# application written in Visual Studio 2012 which uses Excel.Interop technology to read and write Excel files. It has been running fine for years on the current production server (I think it's Server 2016), but we are having trouble setting it up on a new server running Server 2019 Datacenter edition. We installed Office 2016 to get the Interop object, same version as the current server, and the program seems to get as far as creating the Excel application object correctly.
However, when the program tries to open a file, we get an error that it "cannot be accessed".
The problem line, as we suspected and is also confirmed by the stack trace below, is
wbExcelWorkbook = appExcel.Workbooks.Open(txtCurrentDir + "\ExcelTemplate\ReportTemplate.xls", ReadOnly: true);
txtCurrentDir is set to the output of a Server.MapPath(".") call combined with a path that is defined in a configuration table.
The error handler is writing to the Windows application event log, so we can confirm the path and the error message there. Here are those entries from the event log, obtained from properties of the Exception object (e) inside the catch block:
- e.Message: Unable to Open Excel Template - exception = Microsoft Excel cannot access the file 'C:\inetpub\sptdDev\ExcelGen\ExcelTemplate\ReportTemplate.xls'. 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. - e.Source: Microsoft Excel
- e.HelpLink: xlmain11.chm
- e.StackTrace: at Microsoft.Office.Interop.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) at frmSysFunctSubmittalExtract.CreateExcel(Int64 bintSubmittalRecId, SqlConnection sqlCN) in c:\inetpub\sptdDev\frmSysFunctSubmittalExtract.aspx.cs:line 171
- e.Data.Count: 0
- e.InnerException: null
Obviously we checked to make sure the file was present in that server directory, and we are able to open it manually by double-clicking it in the File Manager. (The .xlsx file is there from when we were experimenting with using a newer format file.) We also confirmed it was not read-only or locked by another user, although we don't think that should matter since the program is opening it for read-only anyway.
We used DCOM Config for the "Microsoft Excel Application" to set the identity as the IIS user and set security permissions. This was per an Admin Manual we inherited, and also in consultation with the original author of the program (he still works for our company).
This sysna-sptddev user is a service account we use for this application in the IIS application pool. It is also the account we logged into the server as to look at and open the file in the File Manager.
We are stumped! What else can we look at? Thanks for any help.