You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Framework 4.8构建时Interop.Microsoft.Office.Core报错

问题描述

在高版本.NET Framework环境下构建旧项目时,触发如下错误:
未能加载文件或程序集“Interop.Microsoft.Office.Core”或它的某一个依赖项。试图加载格式不正确的程序。

相关参考材料

程序集绑定错误日志

LOG: DisplayName = Interop.Microsoft.Office.Core
 (Partial.)
WRN: Partial binding information was provided for an assembly:
WRN: Assembly name: Interop.Microsoft.Office.Core | Domain ID: 2
WRN: Partial binding occurs when only part of the assembly display name is provided.
WRN: This may cause the collection folder to load an incorrect assembly.
WRN: Specify the text identity of the assembly with full details. 
WRN: This includes the simple name, version and culture, and public key token.
LOG: Appbase = file:///C:/Users/krebesli/OneDrive - TRUMPF SE + Co. KG/Project - Copy/TruTopsSales/TruTopsSales/
LOG: Original PrivatePath = C:\Users\krebesli/OneDrive - TRUMPF SE + Co. KG\Project - copy\TruTopsSales\TruTopsSales\bin
Call from Assembly : (Unknown).
===
LOG: This binding starts in default load context.
LOG: The application configuration file is used: C:\Users\krebesli\OneDrive - TRUMPF SE + Co. KG\Project - copy\TruTopsSales\TruTopsSales\web.config.
LOG: The host configuration file is used: \srvditz1\home$\krebesli\Documents\IISExpress\config\aspnet.config.
LOG: The computer configuration file of C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config is used.
LOG: policy is not currently applied to the reference (private, custom, partial, or path-based assembly binding).
LOG: download of new URL file:///C:/Users/(username)/AppData/Local/Temp/Temporary ASP.NET Files/vs/2636e7dd/88283cb5/Interop.Microsoft.Office.Core.DLL.
LOG: Download of new URL file:///C:/Users/(username)/AppData/Local/Temp/Temporary ASP.NET Files/vs/2636e7dd/88283cb5/Interop.Microsoft.Office.Core/Interop.Microsoft.Office.Core.DLL.
LOG: Download from new URL file:///C:/Users/(username)/.../.../.../bin/Interop.Microsoft.Office.Core.DLL.
ERR: Assembly setup could not be completed (hr = 0x8007000b). The search was terminated.

当前引用配置

  • Microsoft.Office.Core引用通过Microsoft Office 16.0 Object Library添加
  • Microsoft.Office.Interop.Excel引用通过Microsoft Excel 16.0 Object Library添加
  • Microsoft.Office.Interop.Word引用通过Microsoft Word 16.0 Object Library添加

问题根因

错误码0x8007000b对应程序集加载位宽不匹配,结合绑定日志中加载的是64位.NET Framework运行时配置,核心问题有三点:

  1. 项目生成平台目标与本地安装的Office位宽不一致
  2. 手动通过COM选项卡生成的互操作程序集缺少完整版本、公钥令牌信息,触发部分绑定警告
  3. 旧的编译残留文件导致错误版本的Interop程序集被优先加载

解决方案

按以下步骤依次操作即可解决问题:

1. 全量清理编译残留

  • 关闭Visual Studio,手动删除项目根目录下的bin、obj文件夹全部内容
  • 清空临时ASP.NET编译目录:C:\Users\<你的用户名>\AppData\Local\Temp\Temporary ASP.NET Files\下所有子文件夹内容

2. 统一平台目标配置

  • 右键项目 → 选择「属性」→ 切换到「生成」选项卡
  • 找到「平台目标」下拉选项:
    • 本地安装64位Office时,选择x64,取消勾选「首选32位」
    • 本地安装32位Office时,选择x86,勾选「首选32位」
    • 禁止使用Any CPU配置,避免运行时自动匹配位宽导致加载错误
  • Web项目额外配置IIS Express位宽:点击Visual Studio顶部调试按钮旁的IIS Express下拉菜单 → 显示所有应用 → 选中当前项目 → 配置,64位Office场景下勾选「使用64位版本的IIS Express」

3. 替换手动生成的COM引用

手动从COM选项卡生成的Interop程序集存在元数据缺失问题,按以下步骤替换:

  • 右键项目「引用」列表,移除现有的Microsoft.Office.Core、Microsoft.Office.Interop.Excel、Microsoft.Office.Interop.Word三个引用
  • 打开NuGet包管理器,搜索并安装对应16.0版本的官方互操作包:
    • Microsoft.Office.Core
    • Microsoft.Office.Interop.Excel
    • Microsoft.Office.Interop.Word
      也可以直接在包管理器控制台执行以下命令安装:
    Install-Package Microsoft.Office.Core -Version 16.0.0
    Install-Package Microsoft.Office.Interop.Excel -Version 16.0.0
    Install-Package Microsoft.Office.Interop.Word -Version 16.0.0
    

4. 校验配置文件绑定规则

打开项目根目录的web.config,找到<runtime>节点下的<assemblyBinding>段,删除所有关于Interop.Microsoft.Office.Core的旧绑定规则,确保不存在部分绑定的配置项。安装NuGet包后会自动生成完整的带公钥令牌、版本号的绑定规则,无需手动修改。

5. 修复Office安装

如果以上步骤操作后仍报错,打开控制面板 → 程序和功能 → 找到Microsoft Office安装项 → 点击「更改」→ 选择「快速修复」,修复完成后重启设备再重新生成项目。


内容的提问来源于stack exchange,提问作者Linus _20

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 20:36:10