Reputation: 975
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"/>
Upvotes: 10
Views: 34706
Reputation: 1
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;
}
Upvotes: 0
Reputation: 649
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.
Upvotes: 0
Reputation: 1014
This worked for me:
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.
Upvotes: 2
Reputation: 33
I have the same issue. I search for "ClosedXML.Signed" in the Nugget, update "DocumentFormat.OpenXml" and it works!
Upvotes: 1
Reputation: 425
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);
Upvotes: 1
Reputation: 975
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+
Upvotes: 6