I am making a service to parse through excel files using C# with Visual Studio 2019 using .Net. However, I am getting an error when trying to open an excel file using interop.Excel
The error I am getting is:
System.Runtime.InteropServices.COMException (0x800A03EC): 0x800A03EC
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)
The error occurs on the line where the excel file is opened. I have Microsoft Office installed on the machine along with Excel 2016, so I'm unsure of what the issue is. The weird thing is that other people I know can use interop.Excel in their service, but for me it gives an error.
What I have done so far:
- Made sure the dependencies were installed
- Ran the code as a console application
Adding the dependencies, the start function in the Console Application was able to open the file.
- Used different versions of .Net
I used .Net Core 5.0, and used .Net FrameWork 4, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.7.2. None of them were able to open the file
I read that you can add DependOnService in the service. I have tried both with and without the .dll extension.
- Checked to make sure the Interop.Microsoft.Office.Interop.Excel dll is in the same location as the other dll files (the same directory where the FileParser.exe is)
Here is a sample of code where the error occurs:
using System;
using System.Diagnostics;
using _Excel = Microsoft.Office.Interop.Excel;
namespace FileParser
{
public partial class Service1
{
//Declare logger
//private static Logger Logger = Logger.Instance;
public Service1()
{
InitializeComponent();
}
//Version number
private const string version = "1.0.1.0";
protected void OnStart(string[] args)
{
//Logger.UpdateLog($"Starting Service v{version}", EventLogEntryType.Information);
try
{
//Display that the program started correctly
//Logger.UpdateLog("Running", EventLogEntryType.Information);
//Open the excel file
Microsoft.Office.Interop.Excel.Application x1App = new _Excel.Application();
_Excel.Workbook x1Workbook = x1App.Workbooks.Open(@"C:/Users/strongb2/Downloads/test.xls"); //Error happens here
//Display that the excel file has been opened
//Logger.UpdateLog("File has been opened", EventLogEntryType.Information);
}
catch (Exception e)
{
//Display the error
//Logger.UpdateLog(e.ToString(), EventLogEntryType.Error);
}
}
//Stop function
protected void OnStop()
{
//Show that the service has stopped
//Logger.UpdateLog("Stopping Service", EventLogEntryType.Information);
}
}
}
I'm not sure what else to try to get the file to open using interop.Excel. Does anyone know of a solution to this problem?