.NET Core 3.0和MS Office Interop

编程入门 行业动态 更新时间:2024-10-27 20:27:30
本文介绍了.NET Core 3.0和MS Office Interop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

$ b $中的所有指定框架兼容b

暗示兼容但不完全兼容

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项目生成的引用添加到< ItemGroup> 标签。

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

本文发布于:2023-11-17 06:35:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609172.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Core   NET   Interop   Office   MS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!