What NuGet package to I use for Microsoft.Office.Interop.Excel?

Falanga, Rod, DOH 245 Reputation points
2024-05-07T21:01:02.6533333+00:00

I maintain an old ASP.NET WebForms application, which is at .NET Framework level 4.5.2. The original developers incorporated an Excel report in this application. I'm trying to get the app to build using a GitHub Self-Hosted Runner running on one of our internal servers. It is failing to build. The app has Microsoft.Office.Interop.Excel, version 15.0.4795.1001. However, the log says it cannot find Microsoft.Office.Interop.Excel. My guess is that the app has the wrong version number, but I don't know how to take the version number on NuGet and make it equal to what Visual Studio is expecting. Here's what I've looked at on NuGet: https://www.nuget.org/packages/Microsoft.Office.Interop.Excel#readme-body-tab

Here's a snippet from the VBPROJ file that uses Microsoft.Office.Interop.Excel:

    <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>
    <COMReference Include="Microsoft.Office.Interop.Excel">
      <Guid>{00020813-0000-0000-C000-000000000046}</Guid>
      <VersionMajor>1</VersionMajor>
      <VersionMinor>9</VersionMinor>
      <Lcid>0</Lcid>
      <WrapperTool>primary</WrapperTool>
      <Isolated>False</Isolated>
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </COMReference>

Microsoft 365 and Office | Development | Other
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 60,326 Reputation points
    2024-05-07T21:34:06.88+00:00

    It's not using NuGet. It is using COM interop. Therefore the COM object (Office in this case) would have been installed on the development machine and then it can be referenced directly. At build time the compiler loads the typelib for the COM object and generates the necessary interop code and then inserts it into the assembly rather than requiring a separate assembly.

    If you want to switch to using NuGet (recommended) then you'll need to do the following.

    1. Remove the COM reference from the project.
    2. Add a reference to the NuGet package for the lowest version of Office you need to work against.
    3. Fix up any compiler errors caused by switching from the raw COM reference to the wrapper NuGet package. It might be as simple as adjusting namespaces (COM references had a weird syntax).

    Note that in order to run this code you're still going to need to have Office installed so switching to NuGet would only really be helpful if you are trying to build on a build server that doesn't have Office installed. I would personally still recommend using NuGet instead of COM but it'll fail at runtime without Office installed.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.