0

I would like to have an endpoint in my .NET Core 3.1 API where I open an template Excel file, edit a few cells, and then return the edited Excel file as stream.

I thought I would use Microsoft.Office.Interop.Excel but when adding the reference via the Nuget packages I'm getting the following error:

Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'

After adding it as COM reference, I'm getting the following error:

Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154 Klasse is niet geregistreerd (0x80040154 (REGDB_E_CLASSNOTREG)).

I also don't want to save the edited template, it should be saved as stream so I can return the Excel stream.

Does somebody know if it is possible what I want to do, and yes, how?

Niels
  • 416
  • 5
  • 22
  • While Com is supported in .net core you should really think about the implications: Office in web services is a licensing hell, Office will fail with nasty errors on multiple reasons triggered by a web application (e.g. when some update or license popup is shown in excel) – stefan.seeland Jun 17 '21 at 08:48

1 Answers1

0

asp.net core is platform independent and won't work well with Windows assemblies without various configuration. Now I'm sure you can make it work in 3.1, maybe if you mess with COMReferences as explained in this answer: .NET core 3.0 and MS Office Interop

But what I did to use interop in an asp.net core app, was change the target framework to .net5 instead.

If you are able to use net5.0, then add this to the .csproj file and it should work:

<TargetFramework>net5.0-windows</TargetFramework>
<UseWPF>true</UseWPF>

It might also work to just include <UseWPF>true</UseWPF> in netcoreapp3.1, but I haven't tested this.

Jonas Mohammed
  • 377
  • 4
  • 14