0

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

  1. Service1.cs
  2. Program.cs
  3. 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?

strongb2
  • 1
  • 1
  • 3
    interop works with office installed on the machine. have you installed office with excel on the machine? – Immanuel Jul 07 '21 at 19:30
  • 2
    **1.** See the comment above ^^. Then you need to go and open the Excel and get rid of all the dialogs that popup when you open it for the first time. **2.** What is the motivation here? If you are only reading/storing data, Interop is the worst thing you can do. Especially in the service. Imagine that you have multiple threads/requests, each will need to open an Excel. There are many libraries that will do better job than Interop – T.S. Jul 07 '21 at 20:22
  • The following may be helpful: https://stackoverflow.com/questions/18511576/reading-excel-file-using-oledb-data-provider and https://www.connectionstrings.com/excel/ – Tu deschizi eu inchid Jul 08 '21 at 02:02
  • @Immanuel Microsoft office is installed, and excel 2016 is installed on the machine – strongb2 Jul 08 '21 at 22:49
  • @T.S. 1) Microsoft office is installed on the machine, and excel 2016 is on it as well. 2) I am reading data picking out specific data out of the files and sending them to an API. The reason why I'm trying to get interop to work on this machine is because there are already functions that I have written in the past that will work for this project. I don't want to rewrite code unless I have to haha. – strongb2 Jul 08 '21 at 22:53
  • 1
    The account the server is using probably doesn't have access to your user folder, because that user folder is only accessible to your account. (That's why it has your name in it.) Excel interop is horrible for a service, because the service has no access to the desktop, and Excel without using any of its visual functionality is a total waste of resources when there are better solutions available. Learn to use other tools that are more appropriate for the task. – Ken White Jul 09 '21 at 00:14
  • 1
    @KenWhite This is right. Service must run as specific real User. Besides, Microsoft does not recommend, guaranteed it to work or supports interop for services. ***"Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment. For more information, please see this KB article: Considerations for server-side Automation of Office.*** – T.S. Jul 09 '21 at 13:39
  • @strongb2 See my comment above. If you have interop in service issue - you're on your own. -- Microsoft – T.S. Jul 09 '21 at 13:40
  • @KenWhite Alright thank you, I will look for other options – strongb2 Jul 09 '21 at 16:11
  • 1
    @T.S. Thanks for all the help! I won't use interop and will find a different library to read the files :). – strongb2 Jul 09 '21 at 16:13

0 Answers0