I'm getting a validation error when I run OpenXmlValidator.Validate()
.
The 'http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac:knownFonts' attribute is not declared.
I use the following code to register my fonts.
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;
}
And my style.xml looks like this:
<?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>
Can anyone see why I would get this error? I can't even find information on the purpose of the KnownFont
attribute. Could it be related to the fact that I have x14ac:knownFonts
instead of ac:knownFonts
?
From the 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).
Such an ignore is set up when instantiating a Stylesheet
object.
You have te set the Ignorable
property to the same xml
namespace prefix, being used to register the http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac
namespace.
(It's not required to be x14ac
.)
Stylesheet stylesheet = new Stylesheet()
{
MCAttributes = new MarkupCompatibilityAttributes()
{
Ignorable = "x14ac"
}
};
stylesheet.AddNamespaceDeclaration("x14ac", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac");
Now Styles.xml
will look like below, and the validation error will disappear.
<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>