Excel的最小样式表打开XML的date?

我正在尝试使用Open XML SDK创build一个Excel文件,并使用最less的样式表,这将允许我将单元格格式化为date。 以下是我在样式表中的尝试:

<?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> 

即使没有从单元格中引用此样式,Excel也会告诉我我的文件已损坏。 如果我删除cellXfs元素,文件打开罚款。 有人可以解释我还需要添加到这个? 下面是创build样式表的C#代码。

 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); 

我已经尝试添加单元格样式和单元格样式,但只是有上述似乎打破了文件,我不确定为什么。

通过大量的试验和错误,我发现一个样式表需要以下内容:

  • 默认字体
  • 默认填充
  • 默认边框
  • 默认单元格格式

如果不把它们排除在外,将导致Excel出错,除非所有这些都被排除在外。 另外,我为date添加了另一种单元格格式。

我希望这对其他人有用。 下面的代码创build一个工作样式表,它允许使用date格式(数字格式为22):

 var stylesheet = new Stylesheet(); // Default Font var fonts = new Fonts() { Count = 1, KnownFonts = BooleanValue.FromBoolean(true) }; var font = new Font { FontSize = new FontSize() { Val = 11 }, FontName = new FontName() { Val = "Calibri" }, FontFamilyNumbering = new FontFamilyNumbering() { Val = 2 }, FontScheme = new FontScheme() { Val = new EnumValue<FontSchemeValues>(FontSchemeValues.Minor) } }; fonts.Append(font); stylesheet.Append(fonts); // Default Fill var fills = new Fills() { Count = 1 }; var fill = new Fill(); fill.PatternFill = new PatternFill() { PatternType = new EnumValue<PatternValues>(PatternValues.None) }; fills.Append(fill); stylesheet.Append(fills); // Default Border var borders = new Borders() { Count = 1 }; var border = new Border { LeftBorder = new LeftBorder(), RightBorder = new RightBorder(), TopBorder = new TopBorder(), BottomBorder = new BottomBorder(), DiagonalBorder = new DiagonalBorder() }; borders.Append(border); stylesheet.Append(borders); // Default cell format and a date cell format var cellFormats = new CellFormats() { Count = 2 }; var cellFormatDefault = new CellFormat { NumberFormatId = 0, FormatId = 0, FontId = 0, BorderId = 0, FillId = 0 }; cellFormats.Append(cellFormatDefault); var cellFormatDate = new CellFormat { NumberFormatId = 22, FormatId = 0, FontId = 0, BorderId = 0, FillId = 0, ApplyNumberFormat = BooleanValue.FromBoolean(true) }; cellFormats.Append(cellFormatDate); stylesheet.Append(cellFormats); return stylesheet; 

生成的XML如下所示:

 <?xml version="1.0" encoding="utf-8"?> <x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> <x:fonts count="1" 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:fonts> <x:fills count="1"> <x:fill> <x:patternFill patternType="none" /> </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="2"> <x:xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" /> <x:xf numFmtId="22" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1" /> </x:cellXfs> </x:styleSheet>