.NET core 3.0 and MS Office Interop

前端 未结 3 1372
说谎
说谎 2020-11-30 14:08

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

I\'ve got a minimal project file



        
相关标签:
3条回答
  • 2020-11-30 14:32

    I had the same issue. I fixed it by opening the reference properties and setting both "Copy Local" and "Embed Interop Types" to "Yes".

    Update: This actually does the same thing as adding these 2 lines to the COM reference in the .csproj file.

    <COMReference Include="Microsoft.Office.Core">
        ...
        <EmbedInteropTypes>True</EmbedInteropTypes>
        <Private>true</Private>
    </COMReference>
    

    The "Private" tag isn't mentioned in the accepted answers, but it prevents a lot of problems.

    0 讨论(0)
  • 2020-11-30 14:34

    The Interop Assemblies are not compatible with .NET Core. You have to use the full framework.

    See also this GitHub Issue

    If you want to programmatically create Office documents, you might want to take a look at the Office OpenXML SDK.

    0 讨论(0)
  • 2020-11-30 14:40

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

    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.

    It should look something similar to:

    <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>
    

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

    Update:

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

    0 讨论(0)
提交回复
热议问题