Failed to apply number format with OpenXML in C #
I am trying to get a column in an excel document I am creating using OpenXML, which needs to be formatted in a friendly way for 12-bit UPC.
For this I am using the following code (based on this question ):
var sp = workbookpart.AddNewPart<WorkbookStylesPart>();
sp.Stylesheet = new Stylesheet {NumberingFormats = new NumberingFormats(), CellFormats = new CellFormats()};
var upcFormatting = new NumberingFormat {NumberFormatId = 164, FormatCode = "000000000000"};
var upcCellFormat = new CellFormat
{
NumberFormatId = upcFormatting.NumberFormatId,
FontId = 0U,
FillId = 0U,
BorderId = 0U,
FormatId = 0U,
ApplyNumberFormat = BooleanValue.FromBoolean(true)
};
sp.Stylesheet.NumberingFormats.AppendChild(upcFormatting);
sp.Stylesheet.CellFormats.AppendChild(upcCellFormat);
sp.Stylesheet.NumberingFormats.Count++;
sp.Stylesheet.CellFormats.Count++;
var styleIndex = sp.Stylesheet.CellFormats.Count;
workbookpart.Workbook.Save();
The above code unfortunately creates a stylesheet that appears to be considered corrupted, looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:numFmts count="1">
<x:numFmt formatCode="000000000000" numFmtId="164"/>
</x:numFmts>
<x:cellXfs count="1">
<x:xf numFmtId="164" applyNumberFormat="1" xfId="0" borderId="0" fillId="0" fontId="0"/>
</x:cellXfs>
</x:styleSheet>
If anyone could please explain how I can get a valid stylesheet generated with a numbering format that allows 12 digits to be displayed using OpenXML only (you can't use any frameworks built around it like ClosedXML) I was would be very grateful.
Thank,
+3
source to share