gpt4 book ai didi

c# - 无法使用 Microsoft.Office.Interop.Excel 加载文件或程序集“office,版本 = 15.0.0.0”

转载 作者:行者123 更新时间:2023-12-04 19:54:02 27 4
gpt4 key购买 nike

我正在使用 Microsoft.Office.Interop.Excel 将 excel 转换为 pdf。但是当我启动 Excel 应用程序时,发生了这个错误。我已经在计算机上安装了 Excel 2013。 (我使用的是 VS2019,Window 10)。

我的 Excel 的位置是 C\Program Files (x86)\Microsoft Office\Office 15\Excel

Could not load file or assembly ‘office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxx’. The system cannot find the file specified

欢迎提出任何建议!

这是我的代码:

using Microsoft.Office.Interop.Excel;
using System;
using System.Runtime.InteropServices;

namespace ExcelToPdf
{
public class ExcelApplicationWrapper : IDisposable
{
public Application ExcelApplication { get; }

public ExcelApplicationWrapper()
{
ExcelApplication = new Application(); // start excel application
}

public void Dispose()
{
// Each file I open is locked by the background EXCEL.exe until it is quitted
ExcelApplication.Quit();
Marshal.ReleaseComObject(ExcelApplication);
}
}
}
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

namespace ExcelToPdf
{
public class ExcelInteropExcelToPdfConverter
{
public void ConvertToPdf(IEnumerable<string> excelFilesPathToConvert)
{
using (var excelApplication = new ExcelApplicationWrapper()) // got error here
{

}
}
}
}

最佳答案

这里是答案 https://github.com/dotnet/project-system/issues/5735

为了解决这个问题,项目系统需要添加 True。

旧的:

<ItemGroup>
<COMReference Include="Microsoft.Office.Excel.dll">
<Guid>00020813-0000-0000-c000-000000000046</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>7</VersionMinor>
<WrapperTool>tlbimp</WrapperTool>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
</COMReference>
</ItemGroup>

新:

<ItemGroup>
<COMReference Include="Microsoft.Office.Interop.Excel">
<Guid>{00020813-0000-0000-C000-000000000046}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>7</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>

关于c# - 无法使用 Microsoft.Office.Interop.Excel 加载文件或程序集“office,版本 = 15.0.0.0”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59067072/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com