首页 > 解决方案 > 无法将 Interop Excel 与服务一起使用

问题描述

我正在使用 C# 和 Visual Studio 2019 使用 Dot Net 提供一项服务来解析 excel 文件。我必须同时打开 .xlsx 和 .xls 文件。我打算使用 OpenXml 来读取 excel 文件,但 OpenXml 不读取 .xls 文件。

我得到的错误是:

System.Runtime.InteropServices.COMException (0x800A03EC):Microsoft.Office.Interop.Excel.Workbooks.Open 中的 0x800A03EC(字符串文件名、对象 UpdateLinks、对象只读、对象格式、对象密码、对象 WriteResPassword、对象 IgnoreReadOnlyRecommended、对象来源、对象分隔符、对象可编辑、对象通知、对象转换器、对象 AddToMru、对象本地、对象 CorruptLoad)

错误发生在打开 excel 文件的行。我在机器上安装了 Microsoft Office 和 Excel 2016,所以我不确定问题是什么。奇怪的是,我认识的其他人可以在他们的服务中使用 interop.Excel,但对我来说它给出了一个错误。

当我无法在服务中打开文件时,我尝试使用包 topShelf 使其工作。

我有 3 节课

  1. 服务1.cs
  2. 程序.cs
  3. Logger.cs(没有问题)

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);
        }
    }

}

程序代码:

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;
        }

    }
}

到目前为止我做了什么:

[这是依赖项的样子] https://i.stack.imgur.com/FvS5h.png

添加依赖项后,控制台应用程序中的启动功能能够打开该文件。

作为应用程序运行,相同的代码能够打开文件

我使用了 .Net Core 5.0,并使用了 .Net FrameWork 4、4.5、4.5.1、4.5.2、4.6、4.6.1、4.7.2。他们都无法打开文件

我读到您可以在服务中添加 DependOnService。 https://i.stack.imgur.com/pUY0R.png 我已经尝试过使用和不使用 .dll 扩展名。

我在互联网上看到的任何东西都没有帮助解决这个问题。

我不确定还有什么可以尝试使用 interop.Excel 打开文件。有谁知道这个问题的解决方案?

标签: c#.netexcel

解决方案


推荐阅读