I am making a service to parse through excel files using C# with Visual Studio 2019 using Dot Net. I have to open both .xlsx and .xls files. I was going to use OpenXml to read the excel files instead, but OpenXml doesn't read .xls files.
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.
When I couldn't get the file to open in the service I tried making it work using the package topShelf.
I have 3 Classes
- Service1.cs
- Program.cs
- Logger.cs (No issues)
The code for Service1:
using _Excel = Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System;
using System.Timers;
namespace FileParser
{
class Service1
{
//Version of the program
private const string version = "1.0.1.5";
//Timer to determine how often files are parsed
private System.Timers.Timer myTimer = new System.Timers.Timer();
//Windows Application Log for the application
private static Logger Logger = Logger.Instance;
public Service1()
{
}
public void Start()
{
Logger.UpdateLog($"Starting Service v{version}", EventLogEntryType.Information);
try
{
//Display that the program started correctly
Console.WriteLine("Running");
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 happened here
//Display that the excel file has been opened
Console.WriteLine("File has been opened");
Logger.UpdateLog("File has been opened", EventLogEntryType.Information);
}
catch(Exception e)
{
GorillaLogger.UpdateLog(e.ToString(), EventLogEntryType.Error);
Console.WriteLine(e);
}
}
public void Stop()
{
Logger.UpdateLog("Stopping Service", EventLogEntryType.Information);
}
}
}
Code for Program:
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Topshelf;
namespace FileParser
{
class Program
{
private static Logger Logger = Logger.Instance;
public static void Main()
{
var rc = HostFactory.Run(x =>
{
x.Service<Service1>(s =>
{
s.ConstructUsing(name => new Service1());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
x.RunAsLocalSystem();
x.SetDescription("File opening test");
x.SetDisplayName("File Parser");
x.SetServiceName("FileParser");
});
var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());
Environment.ExitCode = exitCode;
}
}
}
What I have done so far:
- Made sure the dependencies were installed
[Here is what the Dependencies look like] https://i.stack.imgur.com/FvS5h.png
- Ran the code as a console application
adding the dependencies, the start function in the Console Application was able to open the file.
- Ran topShelf as a application as well
Running as application, same code was able to open the file
- Used different versions of Dot 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
- Registry Editor
I read that you can add DependOnService in the service. https://i.stack.imgur.com/pUY0R.png I have tried both with and without the .dll extension.
Checked to make sure the Interop.Microsoft.Office.Interop.Excel file The file is in the same directory where the FileParser.exe is. It is with all the other .dll files.
Googled the error
Nothing I have seen on the internet has helped with this issue.
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?