22

I'm keep getting this error is VS2013

Could not load file or assembly 'DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.

In my web.config

    <?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
    <connectionStrings>
        <add name="hjkom_med_web_udvConnectionString" connectionString="Data Source=M95;Initial Catalog=hjkom-med_web;Persist Security Info=True;User ID=HJkom-MED_web;Password=bvkeB7hh" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <system.web>
        <compilation debug="true" targetFramework="4.0">
            <assemblies>
                <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                **<add assembly="DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>**

</assemblies></compilation>
    </system.web>
</configuration>

I have tried to insert windowsbase and a lot of other things.

I have search and search for a solution, do any body know how to fix it?

Jerodev
  • 32,252
  • 11
  • 87
  • 108
SanderDk
  • 223
  • 1
  • 2
  • 6
  • 7
    Well do you *have* that version of the assembly available? – Jon Skeet Aug 14 '14 at 11:50
  • I'm not quite sure what you mean? it is a file I can add? Perhaps it would help to explain that it is a great system that is and run on an IIS server today, where I'm making a larger amount of corrections which must then be online when I'm done. The error comes when I want to run it in VS2013. – SanderDk Aug 14 '14 at 12:18
  • It's an assembly - it doesn't just appear out of the ether. You'll need to download or install it appropriately. I'd just use the nuget package, to be honest... – Jon Skeet Aug 14 '14 at 12:20
  • Up for Jon, check if you have the version you are trying to include available in the GAC where it should be put when you installed the SDK. If you keep having problems, try using [dependency walker](http://www.dependencywalker.com/) and see if you are missing something else. You can also try setting Copy Local = true on the reference to the .dll and see if that resolves your problem. BTW: I think that a reference to System.Xml is also needed. – Andrej Kikelj Aug 14 '14 at 12:22
  • Jon- I have tried using the NuGet console and it says: PM> Install-Package DocumentFormat.OpenXml Install-Package : The current environment doesn't have a solution open. At line:1 char:16 + Install-Package <<<< DocumentFormat.OpenXml + CategoryInfo : InvalidOperation: (:) [Install-Package], InvalidOperationException + FullyQualifiedErrorId : NuGetNoActiveSolution,NuGet.PowerShell.Commands.InstallPackageCommand Is it because this is a VS2013 website or do i do something wrong? – SanderDk Aug 14 '14 at 12:24
  • Thanks :-) I now got Documentformat.OpenXML added. but now I have fixed that error but now i god 262 errors, it is like after i added DocumentFormat.OpenXml it "forgot" every other reference – SanderDk Aug 14 '14 at 12:54
  • I am does not know on which stage you are getting this error but if after publishing then the solution could be: Open solution explorer -> References/Bin (where this library included) -> right click on the file -> copy local -> true. this operation will copy your file to local directory and publish it with all other libraries. – genichm Nov 19 '14 at 12:28

8 Answers8

20

I had this problem because I had a new version of the .dll installed on my computer running on localhost and my server was running an old version of the same .dll

I just updated it and everything works well after that.

In your case, install the DocumentFormat.OpenXml version 2.5 available in this microsoft link

Lucas
  • 599
  • 7
  • 20
6

At the time of writing this answer, there are 3 versions of Open XML SDK:

You most probably have referenced in your project DLL version 2.5 which was installed on your machine.
To get required v2.0 assembly, I suggest you to use NuGet I provided above.

HRKoder
  • 1,646
  • 15
  • 11
2

I my case, I had different versions referenced in different projects within my solution. VS's nuget consolidate function helped me here as it not only shows different versions but also makes it easy to update the older ones (right click solution >> "manage nuget packages for solution" >> "Consolidate" tab).

IngoB
  • 2,552
  • 1
  • 20
  • 35
2

Just for adding up a working solution, in my case I was working in my computer and a server, the reference wasn't there, then I added a file that used the reference to the DLL, but when I uploaded to the server the new version, I just copied the files because I can't do a "normal VS deploy", as usual, I didn't update web.config... but... these lines were missing at the end:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="DocumentFormat.OpenXml" publicKeyToken="8fb06cb64d019a17" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.12.3.0" newVersion="2.12.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Drawing.Common" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.2" newVersion="4.0.0.2" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

Maybe you don't need the "Drawing reference" but the OpenXml part did the trick, good luck!

Gabriel G
  • 211
  • 5
  • 7
1

You can look this example.

http://www.aspsnippets.com/Articles/Solution-ASPNet-GridView-Export-to-Excel-The-file-you-are-trying-to-open-is-in-a-different-format-than-specified-by-the-file-extension.aspx

I download the example and i imported the

  • ClosedXML.dll
  • DocumentFormat.OpenXml.dll

dlls (realted dlls is allready in project. And i used them). After that my error is gone. You can try.. I dont know why. But the importent is my project is working right now..

Mahmut EFE
  • 5,137
  • 5
  • 46
  • 56
1

Had the same problem recently.

I have ClosedXML referenced in a library and when I use the library in a console application I get missing reference error.

This is because the DocumentFormat.Excel, ExcelNumberFormat and FastMember.Signed are not copied into the output folder of my console application.

2 solutions :

1) install the closedXML nuget package and all its dependencies in the client (console application project in this case).

2) To have the dll copied you have to reference them in the library. Add the following function to the library and call it from the static constructor:

    /// <summary>
    /// This code is here to embeed the following libraries : 
    /// - DocumentFormat.Excel
    /// - ExcelNumberFormat
    /// - FastMember.Signed
    /// </summary>
    private static void EmbeedNeededLibraries()
    {
        Action<Type> noop = _ => { };
        var lib1 = typeof(DocumentFormat.OpenXml.OpenXmlAttribute);
        var lib2 = typeof(ExcelNumberFormat.NumberFormat);
        var lib3 = typeof(FastMember.ObjectAccessor);
        noop(lib1);
        noop(lib2);
        noop(lib3);
    }
0

Check in Packages.config files, in all your projects with in that solution.It's should be same in all projects inside a solution. for reference purpose

0

See https://docs.telerik.com/reporting/installation-deploying-adomd.net In my case, my project was referencing both Telerik.Reporting.OpenXmlRendering versions; once I removed the older one this error went away.

Dave Mc
  • 1
  • 4