C# Unable to use Interop Excel with a service

strongb2 21 Reputation points
2021-07-09T00:16:38.617+00:00

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

  • Registry Editor

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?

Developer technologies C#
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2021-07-09T02:39:00.403+00:00

    Unfortunately, it seems that such usage is unstable and not supported. See: https://support.microsoft.com/en-us/topic/considerations-for-server-side-automation-of-office-48bcfe93-8a89-47f1-0bce-017433ad79e2.

    Consider some substitutes, such as Open XML, recommended by previous article, or: https://www.bing.com/search?q=alternatives+to+office+interop+excel.

    What do you want to do with Excel files?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.