Home > front end >  Simple console app cannot load fle or assembly `DocumentFormat.OpenXml`
Simple console app cannot load fle or assembly `DocumentFormat.OpenXml`

Time:10-07

I have a console app with the following contents in the project file:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <TargetFramework>netstandard2.0</TargetFramework>
    <RootNamespace>basic_example</RootNamespace>
    <ImplicitUsings>disable</ImplicitUsings>
    <StartupObject>basic_example.SpreadSheetWriterExample</StartupObject>
  </PropertyGroup>
    
  <ItemGroup>
    <PackageReference Include="DocumentFormat.OpenXml" Version="2.14.0" />
  </ItemGroup>

</Project>

And the following in SpreadsheetWriterExample.cs:

using System;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using System.Collections.Generic;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Text;

namespace basic_example
{
   class SpreadSheetWriterExample
   {

      public static void CreateSpreadsheetWorkbook(string filepath)
      {
         // Create a spreadsheet document by supplying the filepath.
         // By default, AutoSave = true, Editable = true, and Type = xlsx.
         Console.WriteLine("creating spreadsheet document");
         try
         {
            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook))
            {
               spreadsheetDocument.Close();
            }
         }
         catch (DirectoryNotFoundException e)
         {
            Console.WriteLine("DirectoryNotFoundException");
         }
         catch (IOException e)
         {
            Console.WriteLine("IOException");
         }
      }

      static void Main(string[] args)
      {
         
         CreateSpreadsheetWorkbook("test.xlsx");
      }
   }
}

When I try to run the solution I get the following error:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'DocumentFormat.OpenXml, Version=2.14.0.0, Culture=neutral, PublicKeyToken=8fb06cb64d019a17' or one of its dependencies. The system cannot find the file specified.

at basic_example.SpreadSheetWriterExample.CreateSpreadsheetWorkbook(String filepath)

at basic_example.SpreadSheetWriterExample.Main(String[] args) in SpreadSheetWriterExample.cs:line 39

I don't really know what is going wrong since I have explicitly set the reference to OpenXml in the project file. Furthermore I can't really follow the stack since the exception just tells me that it's coming from somewhere inside the CreateSpreadSheetWorkbook method.

What is causing this error and how can I fix it?

CodePudding user response:

I've reproduced this issue. I managed to fix it by adding the following to the main project PropertyGroup:

<PropertyGroup>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

Note that if your project targets .NET5.0 or .NET6.0, this is not necessary.

There seems to be an open issue for this on Nuget's GitHub.

Here's the documentation for CopyLocalLockFileAssemblies.

  • Related