I'm trying to create an Excel spreadsheet from scratch using OpenXML and I've got everything working okay (dumping actual values into actual cells), but now I'm trying to apply number formatting to columns and I'm running into a problem. I have styles.xml
that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:numFmts count="12">
<x:numFmt numFmtId="216" formatCode="#,###" />
<x:numFmt numFmtId="217" formatCode="$#,###" />
<x:numFmt numFmtId="218" formatCode="#0.00" />
<x:numFmt numFmtId="219" formatCode="#,###" />
<x:numFmt numFmtId="220" formatCode="#0.0%" />
<x:numFmt numFmtId="221" formatCode="#,###" />
<x:numFmt numFmtId="222" formatCode="#0.0%" />
<x:numFmt numFmtId="223" formatCode="#0.0%" />
<x:numFmt numFmtId="224" formatCode="#0.0%" />
<x:numFmt numFmtId="225" formatCode="#,###" />
<x:numFmt numFmtId="226" formatCode="#,###" />
<x:numFmt numFmtId="227" formatCode="#0.0%" />
</x:numFmts>
<x:cellXfs count="12">
<x:xf numFmtId="216" applyNumberFormat="1" />
<x:xf numFmtId="217" applyNumberFormat="1" />
<x:xf numFmtId="218" applyNumberFormat="1" />
<x:xf numFmtId="219" applyNumberFormat="1" />
<x:xf numFmtId="220" applyNumberFormat="1" />
<x:xf numFmtId="221" applyNumberFormat="1" />
<x:xf numFmtId="222" applyNumberFormat="1" />
<x:xf numFmtId="223" applyNumberFormat="1" />
<x:xf numFmtId="224" applyNumberFormat="1" />
<x:xf numFmtId="225" applyNumberFormat="1" />
<x:xf numFmtId="226" applyNumberFormat="1" />
<x:xf numFmtId="227" applyNumberFormat="1" />
</x:cellXfs>
</x:styleSheet>
But Excel doesn't seem to like it and removes it after "repairing" the file. What am I missing here? The docs are a little spotty on exactly what is needed to keep Excel happy.
I manually assigned the numFmtId
starting at what I thought might be a suitably high number. Is that the right way to do it?
Also, I'm aware that the formatCode
are duplicated, but I'd assumed Excel wouldn't get tripped up by that, I could consolidate them if necessary.
My column definitions look like this (in sheet.xml
):
<x:cols>
<x:col min="1" max="1" width="7" />
<x:col min="2" max="2" width="58" />
<x:col min="3" max="3" width="16" style="0" />
<x:col min="4" max="4" width="6" style="1" />
<x:col min="5" max="5" width="17" style="2" />
<x:col min="6" max="6" width="16" style="3" />
<x:col min="7" max="7" width="18" style="4" />
<x:col min="8" max="8" width="17" style="5" />
<x:col min="9" max="9" width="20" style="6" />
<x:col min="10" max="10" width="21" style="7" />
<x:col min="11" max="11" width="21" style="8" />
<x:col min="12" max="12" width="16" style="9" />
<x:col min="13" max="13" width="16" style="10" />
<x:col min="14" max="14" width="19" style="11" />
</x:cols>
For comparison - here's a snippet from a working styles.xml
file created by Excel itself:
<numFmts count="1">
<numFmt numFmtId="164" formatCode="#,##0\p"/> // where does 164 come from?
</numFmts>
<cellXfs count="3">
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/> // do you always need a 0 xf entry? It isn't referenced in the sheet.xml file
<xf numFmtId="3" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/> // assuming numFmtId = 3 is a built in format??
<xf numFmtId="164" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
</cellXfs>