C# Microsoft.Office.Interop.Excel Could not load file Error

Error desc:

Could not load file or assembly ‘office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ or one of its
dependencies. The system cannot find the file specified.

使用Microsoft.Office.Interop.Excel导出到excel文件时碰到的错误,Google了很多,包括dll和更换版本(office、nuget)的解决方法都试过,没有解决,最终对比之前写的项目,解决了问题。

Fixed:
我的项目名为Testing20221031
新建的项目根目录下有个Testing20221031.csproj,下载Microsoft.Office.Interop.Excel的NuGet后打开应该是这样的:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
  
  <ItemGroup>
    <PackageReference Include="Microsoft.Office.Interop.Excel" Version="12.0.4518.1014" />
    <PackageReference Include="NPOI" Version="2.5.6" />
  </ItemGroup>

</Project>

对照之前的项目更改完后是这样:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

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

  <ItemGroup>
    <PackageReference Include="NPOI" Version="2.5.6" />
  </ItemGroup>

</Project>

删了version,加了个ItemGroup,问题解决。

====================================================
附:NPOI是另一个NuGet,另一种导出excel数据的实现方法的库,和Microsoft.Office.Interop.Excel没有关系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值