.NET core 3.0 和 MS Office Interop [英] .NET core 3.0 and MS Office Interop

查看:31
本文介绍了.NET core 3.0 和 MS Office Interop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用互操作程序集在 MS Office 中使用最近发布的 .NET 核心

I am trying to use the recently-released .NET core with MS Office using the interop assemblies

我有一个最小的项目文件

I've got a minimal project file

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

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

  <ItemGroup>
    <PackageReference Include="Microsoft.Office.Interop.Word">
      <Version>15.0.4797.1003</Version>
    </PackageReference>
  </ItemGroup>

</Project>

和这个 C# 程序

using System;
using Microsoft.Office.Interop.Word;
namespace ii
{
    class Program
    {
        static void Main(string[] args)
        {
            var app = new Application();
            Console.WriteLine(app.Version);
            app.Quit();
        }
    }
}

不幸的是这失败了

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified.
File name: 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'

当我将包添加到项目中时,我得到了这个

When I added the package to the project I got this

warn : Package 'Microsoft.Office.Interop.Word 15.0.4797.1003' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v3.0'. This package may not be fully compatible with your project.
info : Package 'Microsoft.Office.Interop.Word' is compatible with all the specified frameworks in project 

暗示兼容"而不是完全兼容"

implying 'compatible' but not 'fully compatible'

有没有办法做到这一点,或者我必须使用 .NET Framework 而不是 Core?

Is there a way to do this or must I use .NET Framework instead of Core?

我使用的是 Windows 10、.NET core 3.0.100 和 MS Office 365(Word 版本为 16.0.11929.20298)

I am using Windows 10, .NET core 3.0.100 and MS Office 365 (Word is version 16.0.11929.20298)

推荐答案

这个问题的解决方案有点古怪,但可行.

The solution to this is a bit quirky, but possible.

创建一个新的 .NET Framework 4.X 项目.将相关的 COM 引用添加到项目中.编辑 .NET Core 3.0 项目的 .csproj 并将从 .NET Framework 项目生成的引用添加到 标记.

Create a new .NET Framework 4.X project. Add the relevant COM references to the project. Edit the .csproj of your .NET Core 3.0 project and add the generated references from the .NET Framework project to the <ItemGroup> tag.

它应该类似于:

<ItemGroup>
    <COMReference Include="Microsoft.Office.Core">
      <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
      <VersionMajor>2</VersionMajor>
      <VersionMinor>8</VersionMinor>
      <Lcid>0</Lcid>
      <WrapperTool>primary</WrapperTool>
      <Isolated>False</Isolated>
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </COMReference>

... more references

</ItemGroup>

不要使用 NuGet 包,它们与 .NET Core 不兼容.

Do not use the NuGet packages, they are not compatible with .NET Core.

更新:

您现在可以直接从 IDE 添加 COM 引用(自 Visual Studio 2019 v16.6 起):

You can now add the COM references straight from the IDE (since Visual Studio 2019 v16.6):

这篇关于.NET core 3.0 和 MS Office Interop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆