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

问题描述 投票:0回答:9

我一直收到此错误是 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和很多其他东西。

我一直在寻找解决方案,有人知道如何解决吗?

c# xml web visual-studio-2013 config
9个回答
20
投票

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

我刚刚更新了它,之后一切正常。

对于您的情况,请安装此 microsoft 链接

中提供的 DocumentFormat.OpenXml 版本 2.5

6
投票

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

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


2
投票

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


2
投票

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

  <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 部分可以解决问题,祝你好运!


1
投票

你可以看看这个例子。

http://www.aspsnippets.com/Articles/Solution-ASPNet-GridView-Export-to-Excel-The-file-you-are-trying-to-open-is-in-a- different-format-比文件扩展名指定的.aspx

我下载了示例并导入了

  • ClosedXML.dll
  • DocumentFormat.OpenXml.dll

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


1
投票

最近也遇到同样的问题。

我在库中引用了 ClosedXML,当我在控制台应用程序中使用该库时,出现缺少引用错误。

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

2 个解决方案:

1) 在客户端(本例中为控制台应用程序项目)中安装 closeXML 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);
    }

0
投票

检查该解决方案中的所有项目中的 Packages.config 文件。它在解决方案内的所有项目中应该是相同的。仅供参考


0
投票

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


0
投票

我已将 DocumentFormat.XML dll 引用到我的 .Net Core Web Api 项目中,并通过 xCopy 进行部署。问题是我只复制了 dll 文件并忽略了自动生成的 json 文件:“<>.deps.json” - 它实际上列出了项目的所有依赖项。一旦我将此文件包含在 xCopy 中,它就起作用了。

© www.soinside.com 2019 - 2024. All rights reserved.