I'm trying to create an Excel file using the Open XML SDK with the minimal style sheet needed that will allow me to format a cell as a date. Below is my attempt at a style sheet:
<?xml version="1.0" encoding="utf-8"?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:cellXfs count="1">
<x:xf numFmtId="14" xfId="0" applyNumberFormat="1" />
</x:cellXfs>
</x:styleSheet>
Even without referencing this style from a cell, Excel tells me that my file is corrupt. If I remove the cellXfs element, the file opens fine. Can someone explain what else I need to add to this? The C# code that creates the style sheet is below.
var stylesheet = new Stylesheet();
var cellFormats = new CellFormats() { Count = 1 };
var cellFormat = new CellFormat();
cellFormat.NumberFormatId = 14;
cellFormat.FormatId = 0;
cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true);
cellFormats.Append(cellFormat);
stylesheet.Append(cellFormats);
I've tried adding in cell style formats and cell styles, but just having the above seems to break the document and I'm unsure why.
Through a lot of trial and error, I've found that a style sheet needs the following:
Leaving any of those out will cause Excel to error, unless all of them are left out. In addition, I added another cell format for the date.
I hope this is useful to someone else. The following code creates a working style sheet that allows for a date format (number format id 22):
The resulting XML looks like: