简体   繁体   English

“无法加载文件或程序集”- Microsoft.Office.Interop.Excel

[英]"Cannot load file or assembly" - Microsoft.Office.Interop.Excel

I have a project targeting.Net 6.0 framework and I am trying to use Microsoft.Office.Interop.Excel as I would like to run an excel macro configured inside the.xlsm file that exists on the same local system.我有一个项目 targeting.Net 6.0 框架,我正在尝试使用 Microsoft.Office.Interop.Excel,因为我想运行在同一本地系统上存在的 .xlsm 文件中配置的 excel 宏。 Regarding Excel version - I am running Microsoft 365 (I am not sure how this translates into Excel versions that used to be more explicit in the naming earlier).关于 Excel 版本 - 我正在运行 Microsoft 365(我不确定这如何转化为 Excel 版本,以前在命名中更明确)。 The error that I have when running my code is shown here:运行我的代码时出现的错误如下所示:
1个

This is the code where the problem occurs (last line basically):这是出现问题的代码(基本上是最后一行):

void RunMacro(string macroFilename)
{
    Excel.Application xlApp = new Excel.Application();
    Excel.Workbook xlWorkBook;
   
    xlWorkBook = xlApp.Workbooks.Open(macroFilename);
    xlApp.Visible = true;

    xlApp.Run("Main");
}

For including Interop.Excel package in the project, I tried to install it as a NuGet package, so in that case the project file looks like this为了在项目中包含 Interop.Excel package,我尝试将其安装为 NuGet package,因此在这种情况下,项目文件如下所示

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="DotNetCore.NPOI" Version="1.2.3" />
    <PackageReference Include="ExcelDataReader" Version="3.6.0" />
    <PackageReference Include="ExcelDataReader.DataSet" Version="3.6.0" />
    <PackageReference Include="Microsoft.Office.Interop.Excel" Version="15.0.4795.1001" />
  </ItemGroup>

</Project>

Alternatively, I tried to refer to the interop dll using the "Add project reference" option, so here is how the project file looks in that case:或者,我尝试使用“添加项目引用”选项来引用互操作 dll,因此在这种情况下项目文件的外观如下:

 <Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="DotNetCore.NPOI" Version="1.2.3" />
    <PackageReference Include="ExcelDataReader" Version="3.6.0" />
    <PackageReference Include="ExcelDataReader.DataSet" Version="3.6.0" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="Microsoft.Office.Interop.Excel">
      <HintPath>..\..\..\..\..\..\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>

However, in the end, the error from the screenshot remains in both cases.然而,最终,屏幕截图中的错误在这两种情况下仍然存在。 What could be causing this?是什么原因造成的? Thanks a lot for any help.非常感谢您的帮助。

You need to have corresponding version of Office installed.您需要安装相应版本的Office。 It means for Interop version 15.0.0.0 you should have Office 2013 and other versions of Office will not work.这意味着对于 Interop 版本 15.0.0.0,您应该拥有 Office 2013,而其他版本的 Office 将无法使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法加载文件或程序集“ Microsoft.Office.Interop.Excel” - Could not load file or assembly 'Microsoft.Office.Interop.Excel' 无法使用 Microsoft.Office.Interop.Excel 加载文件或程序集“office,版本 = 15.0.0.0” - Could not load file or assembly ‘office, Version=15.0.0.0 using Microsoft.Office.Interop.Excel 使用 Microsoft.Office.Interop.Excel 的问题:无法加载文件或程序集 &#39;office,版本 = 15.0.0.0 - Issue using Microsoft.Office.Interop.Excel: Could not load file or assembly 'office, Version=15.0.0.0 无法在Windows XP中加载文件或程序集microsoft.office.interop.excel版本15.0.0.0 - could not load file or assembly microsoft.office.interop.excel version 15.0.0.0 in windows xp 程序集“ Microsoft.Office.Interop.Excel”中的“ Microsoft.Office.Interop.Excel.WorkbookClass”未标记为可序列化 - Microsoft.Office.Interop.Excel.WorkbookClass' in Assembly 'Microsoft.Office.Interop.Excel, is not marked as serializable Microsoft.Office.Interop.Excel或EPPlus,用于读取巨大(或不巨大)的Excel文件 - Microsoft.Office.Interop.Excel or EPPlus for read a huge (or not) Excel file 无法找到Microsoft.Office.Interop.Excel参考 - Microsoft.Office.Interop.Excel Reference Cannot be found Office 2007的Microsoft.Office.Interop.Excel - Microsoft.Office.Interop.Excel for Office 2007 Microsoft.Office.Interop.Excel-* .csv文件打开 - Microsoft.Office.Interop.Excel - *.csv file opening 通过使用Microsoft.Office.Interop.Excel将文件另存为PDF - Saving file as PDF by using Microsoft.Office.Interop.Excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM