10

I am trying to use ClosedXML to produce excel documents within an application however when ever I try to run it I receive a could not load file or assembly server error.

Could not load file or assembly 'DocumentFormat.OpenXml, Version=2.5.5631.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

I have seen several solution to this that include changing the 'Copy Local' value for the .dll in its properties and adding the assembly directly to the web.config.

I'm using Visual Studios 2010 and the dll properties does not include a 'copy local' entry. Then when I enter the following assembly in the web config I still get the same server error message. I got the assembly from the error message provided.

 <add assembly="ClosedXML, Version=0.76.0.0, Culture=neutral, PublicKeyToken=fd1eb21b62ae805b"/>
Ethel Patrick
  • 885
  • 7
  • 18
  • 38
  • 1
    I am getting this error too. Visual Studio 2017, C#, using Nuget ClosedXML and all related dependencies updated. All works well on the Visual Studio 2017 development PC (Windows 10) but fails when I xcopy the release to Windows Server 2016. I do not have this problem xcopy to Windows Server 2012 R2. I hope someone figures it out. – gridtrak Jun 19 '17 at 18:05
  • 1
    I was unable to get the nuget ClosedXML to work in Windows Server 2016 because it gave the "could not load file or assembly..." error. However, when I downloaded the source code for ClosedXML from github and built my application referencing that, all worked well. – gridtrak Jun 19 '17 at 18:37

6 Answers6

5

After going crazy searching for answers and finally posting a question, I came across the simplest answer and once I referenced the additional .dll everything worked correctly.

To use ClosedXML you must reference the DocumentFormat.OpenXml.dll:

DocumentFormat.OpenXml.dll for NET 4.0+

Ethel Patrick
  • 885
  • 7
  • 18
  • 38
1

I got the same issue, but I just added the nuget package and made sure it is referenced correctly in my project by

using ClosedXML.Excel;

Then I can access the excel file with

var wb = new XLWorkbook(MyExcelFile);
TropicalViking
  • 407
  • 2
  • 9
  • 25
1

I have the same issue. I search for "ClosedXML.Signed" in the Nugget, update "DocumentFormat.OpenXml" and it works!

Vincent T.
  • 33
  • 2
1

This worked for me:

https://social.msdn.microsoft.com/Forums/en-US/e6cc73f3-050c-4a0a-b9e5-42bf904a163f/nuget-package-manager-does-not-copy-depenencies-to-output?forum=csharpgeneral

Copying here for posterity:

There is a open issue on this here

https://github.com/NuGet/Home/issues/4837

Try unloading the project, right click on the project, edit. Add the following

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

Save, reload, build.

GisMofx
  • 982
  • 9
  • 27
0

I went through every answer here and nothing worked. The way I resolved this was to rollback ClosedXML to v0.90.0 via the NuGet package manager. My error message then changed to complaining about needing v2.16.0 of the DocumentFormat.OpenXML dependency. So, again, via the Nuget Package Manager, I changed the version of DocumentFormat.OpenXML I had installed to v2.16.0. It then worked. What an awful load of guff to go through to get a simple DLL to work. Oh, I also know for a fact that a couple of months ago it was all working fine, I only discovered that something had changed when I went to use this functionality in my project some months later.

Davy C
  • 639
  • 5
  • 16
0

If you are referencing ClosedXML from a class B that gets called by another application A, then this should be the cause:

When A runs, it only looks for assemblies in a set of folders, it doesn't know where B's dependencies are. If ClosedXML isn't in the same directory as A's executable then you'll likely run into this issue.

See this link for original answer: How to add folder to assembly search path at runtime in .NET?

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(LoadFromSameFolder);

static Assembly LoadFromSameFolder(object sender, ResolveEventArgs args)
{
    string folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    string assemblyPath = Path.Combine(folderPath, new AssemblyName(args.Name).Name + ".dll");
    if (!File.Exists(assemblyPath)) return null;
    Assembly assembly = Assembly.LoadFrom(assemblyPath);
    return assembly;
}