question

strongb2-9577 avatar image
0 Votes"
strongb2-9577 asked Ezreal95-7594 commented

C# Unable to use Interop Excel with a service

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?

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@strongb2-9577
0x800A03EC is an overly widespread error, and there are too many potential causes for this error.

  • An xls file is used, but the number of lines exceeds 65536.

  • Path is too long

  • Locales

  • There is a problem with office software or operating system

There are a lot more...

To be honest, it is difficult for us to accurately determine the cause of your problem, because the cause of the problem is not the code but other factors so that we cannot reproduce the error.

My suggestion is the same as Viorel-1, please consider using other libraries.

1 Vote 1 ·

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Ezreal95-7594 commented

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?

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'm reading the excel files to get the data from specific rows and columns. The goal is to read the data and send it to an API.
Thanks for the help, I will use Open XML and look for another package to read xls files.

0 Votes 0 ·

I suggest that you try Spire.XLS for .NET to read data from Excel documents. Very easy to use. And It offers numerous examples for developers to get started.


0 Votes 0 ·