Could not load file or assembly 'DocumentForma

2020-03-09 08:59发布

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?

5条回答
太酷不给撩
2楼-- · 2020-03-09 09:14

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

查看更多
萌系小妹纸
3楼-- · 2020-03-09 09:23

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.

查看更多
对你真心纯属浪费
4楼-- · 2020-03-09 09:24

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);
    }
查看更多
Explosion°爆炸
5楼-- · 2020-03-09 09:31

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..

查看更多
▲ chillily
6楼-- · 2020-03-09 09:36

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

查看更多
登录 后发表回答