无法加载文件或程序集'DocumentFormat.OpenXml

时间:2014-08-14 11:48:07

标签: c# xml web visual-studio-2013 config

我一直得到这个错误是VS2013

Could not load file or assembly 'DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neut ral,PublicKeyToken = 31bf3856ad364e35'或其中一个依赖项。

在我的 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>

我试图插入windowsbase和很多其他东西。

我有搜索和搜索解决方案,任何人都知道如何修复它吗?

8 个答案:

答案 0 :(得分:10)

我遇到了这个问题,因为我在运行localhost的计算机上安装了新版本的.dll,而我的服务器运行的是旧版本的.dll

我刚刚更新了它,之后一切运行良好。

在您的情况下,安装此microsoft link

中提供的DocumentFormat.OpenXml版本2.5

答案 1 :(得分:4)

在撰写本答案时,有三个版本的Open XML SDK:

您很可能已在您的计算机上安装的DLL版本2.5中引用了该文件 为了获得所需的v2.0程序集,我建议你使用上面提供的NuGet。

答案 2 :(得分:1)

您可以查看此示例。

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

我下载了示例,我导入了

  • ClosedXML.dll
  • DocumentFormat.OpenXml.dll

dlls(已经实现的dll已经在项目中了。我使用它们)。之后我的错误消失了。你可以试试..我不知道为什么。但重要的是我的项目现在正在运作..

答案 3 :(得分:0)

在该解决方案中的所有项目中检入Packages.config文件,解决方案中的所有项目都应相同。供参考

答案 4 :(得分:0)

最近有同样的问题。

我在库中引用了ClosedXML,当我在控制台应用程序中使用该库时,会丢失参考错误。

这是因为DocumentFormat.Excel,ExcelNumberFormat和FastMember.Signed没有复制到控制台应用程序的输出文件夹中。

2个解决方案:

1)在客户端中安装closedXML nuget软件包及其所有依赖项(在这种情况下为控制台应用程序项目)。

2)要复制dll,必须在库中引用它们。 将以下函数添加到库中,然后从静态构造函数中调用它:

    /// <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);
    }

答案 5 :(得分:0)

我的情况是,我在解决方案的不同项目中引用了不同的版本。 VS的nuget合并功能在这里帮助了我,因为它不仅显示了不同的版本,而且还可以轻松更新较旧的版本(右键单击解决方案>>“管理解决方案的nuget程序包” >>“合并”选项卡)。

答案 6 :(得分:0)

请参见https://docs.telerik.com/reporting/installation-deploying-adomd.net 就我而言,我的项目同时引用了两个Telerik.Reporting.OpenXmlRendering版本。一旦我删除了旧版本,这个错误就消失了。

答案 7 :(得分:0)

只是为了添加一个可行的解决方案,在我的情况下,我在我的计算机和服务器上工作,引用不在那里,然后我添加了一个使用对 DLL 的引用的文件,但是当我上传到服务器新版本,我只是复制文件,因为我不能做“正常的 VS 部署”,像往常一样,我没有更新 web.config ......但是......最后缺少这些行:< /p>

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

也许你不需要“绘图参考”,但 OpenXml 部分成功了,祝你好运!