c# - 无法将 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.cs
- 程序.cs
- 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
- 将代码作为控制台应用程序运行
添加依赖项后,控制台应用程序中的启动功能能够打开该文件。
- 将 topShelf 也作为应用程序运行
作为应用程序运行,相同的代码能够打开文件
- 使用不同版本的点网。
我使用了 .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.Microsoft.Office.Interop.Excel 文件 该文件位于 FileParser.exe 所在的同一目录中。它与所有其他 .dll 文件一起使用。
谷歌搜索错误
我在互联网上看到的任何东西都没有帮助解决这个问题。
我不确定还有什么可以尝试使用 interop.Excel 打开文件。有谁知道这个问题的解决方案?
解决方案
推荐阅读
- java - AWS Lambda 每天向 Redshift 查询一次
- typescript - 如何将此数据转换为数组?(Typescript)
- python - cursor.fetchall() 有时会返回一个空列表
- node.js - 如何使用真实设备在本地将flutter应用程序与nodejs连接起来
- ios - 简化运行构建而不是按清洁问题时面临的问题
- go - 为什么用户定义时间的位置为零
- flutter - 可以将 Flutter 用于符合 Hipaa 的应用程序吗?
- php - 在移动 API 中使用 REST API 的 3D 安全充电
- javascript - 如何在页面上显示 JavaScript ID 以及将相同的 ID 发送到另一个页面
- php - 为什么提交成功后数据还没有保存到MySQL