簡體   English   中英

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

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

我有一個項目 targeting.Net 6.0 框架,我正在嘗試使用 Microsoft.Office.Interop.Excel,因為我想運行在同一本地系統上存在的 .xlsm 文件中配置的 excel 宏。 關於 Excel 版本 - 我正在運行 Microsoft 365(我不確定這如何轉化為 Excel 版本,以前在命名中更明確)。 運行我的代碼時出現的錯誤如下所示:
1個

這是出現問題的代碼(基本上是最后一行):

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

    xlApp.Run("Main");
}

為了在項目中包含 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>

或者,我嘗試使用“添加項目引用”選項來引用互操作 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>

然而,最終,屏幕截圖中的錯誤在這兩種情況下仍然存在。 是什么原因造成的? 非常感謝您的幫助。

您需要安裝相應版本的Office。 這意味着對於 Interop 版本 15.0.0.0,您應該擁有 Office 2013,而其他版本的 Office 將無法使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM