代码之家  ›  专栏  ›  技术社区  ›  B. Clay Shannon-B. Crow Raven

如何确保OpenXml程序集不会与SpreadsheetLight发生冲突?

  •  7
  • B. Clay Shannon-B. Crow Raven  · 技术社区  · 8 年前

    I Nuget电子表格灯。为了以后使用它,我需要添加以下用法:

    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Spreadsheet;
    using SpreadsheetLight;
    

    为了识别前两个(“DocumentFormat”),我还需要NuGet Microsoft的“Open XML Format SDK”

    我得到了最新版本的2.5

    然而,即便如此,我还是收到了一条关于需要参考的错误消息:

    类型“DocumentFormat.OpenXml.Spreadsheet”。在未引用的程序集中定义了“InlineString”。必须添加对程序集“DocumentFormat”的引用。OpenXml,Version=2.0.5022.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35'。

    这行SpreadsheetLight代码引发了这条消息:

    sl.SetCellValue("A1", true); // "sl" is an SLDocument
    

    因此,我从我的项目中删除了NuGot(版本2.6.0.0,运行时版本v4.0.30319)的引用,然后通过浏览C:\Program Files(x86)\Open XML SDK\V2.0\lib并选择“DocumentFormat.OpenXml.dll”来添加回引用

    然后我得到了一个编译器警告:

    发现同一依赖程序集的不同版本之间存在冲突。请在项目文件中将“AutoGenerateBindingRedirects”属性设置为true。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=294190 .

    我注意到我从文件系统中添加的DLL是版本2.5.5631.0,而NuGett以这种方式作为参考安装的DLL是2.6.0.0版本。运行时版本也不同(v4.0.30319是由NuGetting“Open XML Format SDK”安装的,但我手动添加的DLL的版本是2.5.5631.0,运行时版本v4.0.303 19

    根据 this ,我认为我应该通过更改来编辑.csproj文件 <AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects> 设置为true-但AutoGenerateBindingRedirects不存在。

    我不知道是否应该添加它,如果应该(在哪个“块”中)。我更倾向于安全行事,安抚警告引擎。如何确保OpenXml程序集不会引起冲突?

    3 回复  |  直到 8 年前
        1
  •  5
  •   B. Clay Shannon-B. Crow Raven    8 年前

    确认该警告(以便它在日落时消失)是一个降级DocumentFormat版本的问题。OpenXML至版本2.0.5022.0(运行时版本v2.0.50727)

    我发现这一点是因为这个代码来自“Hello World”示例 here .

    SLDocument sl = new SLDocument();
    sl.SetCellValue("A1", true);
    . . .
    

    …第一行失败,“ 无法加载文件或程序集“DocumentFormat”。OpenXml,Version=2.0.5022.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35'或其依赖项之一 "

    因此,由于它的预期版本是2.0,我删除了该文件的2.5.5631.0,然后NuGot取而代之的是“OpenXML SDK 2.0”。即版本2.0.5022.0和运行时版本2.0.50727

    所以:毕竟不需要用一个神秘的布尔属性更新项目文件。

    不过,这有点让我不得不使用旧版本的程序集。

    更新

    需要使用DocumentFormat“复古”。OpenXml得到证实 here .

        2
  •  4
  •   Oleg    8 年前

    可以通过重定向来解决问题 DocumentFormat.OpenXml 从版本2.0.5022.0到更新版本,例如到版本 2.5.5631.0 。为此,应添加 web.config 新的 <dependentAssembly> 项目:

    <configuration>
      ...
      <runtime>
          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
             ...
             <dependentAssembly>
                <assemblyIdentity name="DocumentFormat.OpenXml" publicKeyToken="31bf3856ad364e35"/>
                <bindingRedirect oldVersion="1.0.0.0-2.0.5022.0" newVersion="2.5.5631.0"/>
             </dependentAssembly>
          </assemblyBinding>
      </runtime>
      ...
    <configuration>
    
        3
  •  3
  •   Millstream30    6 年前

    电子表格与DocumentFormat配合使用。OpenXml 2.5自3.4.5版起:

    “版本3.4.5 -SmartTags现在不再被考虑(你现在不太聪明了,是吗?;)。这意味着该代码现在可以用于OpenXMLSDK2.5了!是的,它现在可以与OpenXMLSDK2.5一起使用了(我提到过吗?哈哈)”

    引用自: https://www.nuget.org/packages/SpreadsheetLight/