OpenXML 验证错误说 knownFonts 属性未声明,但它是

OpenXML validation error says knownFonts attribute not declared but it is

我在 运行 OpenXmlValidator.Validate() 时遇到验证错误。

The 'http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac:knownFonts' attribute is not declared.

我使用以下代码注册我的字体。

public uint Register(Font font)
{
    Stylesheet stylesheet = Builder.GetStylesheet();
    Fonts fonts = stylesheet.Fonts ?? stylesheet.AppendChild(new Fonts() { KnownFonts = BooleanValue.FromBoolean(true) });
    fonts.Append(font);
    fonts.Count = (uint)fonts.Count();
    return fonts.Count - 1;
}

我的style.xml看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  <x:numFmts count="3">
    <x:numFmt numFmtId="164"
              formatCode="0" />
    <x:numFmt numFmtId="165"
              formatCode="#,##0.###" />
    <x:numFmt numFmtId="166"
              formatCode="m/d/yyyy h:mm AM/PM" />
  </x:numFmts>
  <x:fonts count="4"
           x14ac:knownFonts="1"
           xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
    <x:font>
      <x:sz val="11" />
      <x:name val="Calibri" />
      <x:family val="2" />
      <x:scheme val="minor" />
    </x:font>
    <x:font>
      <x:b />
      <x:sz val="11" />
      <x:name val="Calibri" />
      <x:family val="2" />
      <x:scheme val="minor" />
    </x:font>
    <x:font>
      <x:b />
      <x:sz val="20" />
      <x:name val="Calibri" />
      <x:family val="2" />
      <x:scheme val="minor" />
    </x:font>
    <x:font>
      <x:b />
      <x:sz val="14" />
      <x:name val="Calibri" />
      <x:family val="2" />
      <x:scheme val="minor" />
    </x:font>
  </x:fonts>
  <x:fills count="2">
    <x:fill>
      <x:patternFill patternType="none" />
    </x:fill>
    <x:fill>
      <x:patternFill patternType="gray125" />
    </x:fill>
  </x:fills>
  <x:borders count="1">
    <x:border>
      <x:left />
      <x:right />
      <x:top />
      <x:bottom />
      <x:diagonal />
    </x:border>
  </x:borders>
  <x:cellXfs count="12">
    <x:xf numFmtId="0"
          fontId="0"
          fillId="0"
          borderId="0"
          applyNumberFormat="1" />
    <x:xf numFmtId="3"
          fontId="0"
          fillId="0"
          borderId="0"
          applyNumberFormat="1" />
    <x:xf numFmtId="165"
          fontId="0"
          fillId="0"
          borderId="0"
          applyNumberFormat="1" />
    <x:xf numFmtId="44"
          fontId="0"
          fillId="0"
          borderId="0"
          applyNumberFormat="1" />
    <x:xf numFmtId="166"
          fontId="0"
          fillId="0"
          borderId="0"
          applyNumberFormat="1" />
    <x:xf numFmtId="14"
          fontId="0"
          fillId="0"
          borderId="0"
          applyNumberFormat="1" />
    <x:xf numFmtId="18"
          fontId="0"
          fillId="0"
          borderId="0"
          applyNumberFormat="1" />
    <x:xf fontId="1"
          applyFont="1" />
    <x:xf fontId="2"
          applyFont="1" />
    <x:xf fontId="3"
          applyFont="1" />
    <x:xf fontId="2"
          applyFont="1"
          applyAlignment="1">
      <x:alignment horizontal="right" />
    </x:xf>
    <x:xf fontId="3"
          applyFont="1"
          applyAlignment="1">
      <x:alignment horizontal="right" />
    </x:xf>
  </x:cellXfs>
</x:styleSheet>

谁能看出我为什么会收到此错误?我什至找不到有关 KnownFont 属性用途的信息。这可能与我有 x14ac:knownFonts 而不是 ac:knownFonts 的事实有关吗?

来自documentation

The font element ([ISO/IEC29500-1:2016] section 18.8.22) is extended by the addition of a knownFonts attribute. To maintain compatibility with implementations of Office Open XML file formats as specified in [ISO/IEC29500-1:2016], the namespace prefix of the attribute MUST be specified as an Ignorable attribute ([ISO/IEC29500-3:2015] section 10.1.1).

实例化Stylesheet对象时设置了这样的忽略。
您已将 Ignorable 属性 设置为相同的 xml 命名空间前缀,用于注册 http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac 命名空间。
(不需要 x14ac。)

Stylesheet stylesheet = new Stylesheet() 
{
    MCAttributes = new MarkupCompatibilityAttributes() 
    {
        Ignorable = "x14ac"
    }
};
stylesheet.AddNamespaceDeclaration("x14ac", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac");

现在 Styles.xml 将如下所示,验证错误将消失。

<styleSheet
    xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
    mc:Ignorable="x14ac" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    >
    <fonts x14ac:knownFonts="1" count="1">
    <!-- ... -->
    <font>
</styleSheet>