I'm trying to define a CellStyle within a completly empty spreadsheet. For the CellFormat I use the following code:
var workbookStylesPart = spreadsheetDocument.WorkbookPart.GetPartsOfType<WorkbookStylesPart>().First(); //create default font for links Font font2 = new Font(); Underline underline1 = new Underline(); FontSize fontSize2 = new FontSize() { Val = 11D }; Color color2 = new Color() { Theme = (UInt32Value)10U }; FontName fontName2 = new FontName() { Val = "Calibri" }; FontFamilyNumbering fontFamilyNumbering2 = new FontFamilyNumbering() { Val = 2 }; FontScheme fontScheme2 = new FontScheme() { Val = FontSchemeValues.Minor }; font2.Append(underline1); font2.Append(fontSize2); font2.Append(color2); font2.Append(fontName2); font2.Append(fontFamilyNumbering2); font2.Append(fontScheme2); workbookStylesPart.Stylesheet.Fonts.Append(font2); int fontId = workbookStylesPart.Stylesheet.Fonts.Count() - 1; CellFormat copy = (CellFormat)workbookStylesPart.Stylesheet.CellFormats.FirstOrDefault().Clone(); copy.FontId = Convert.ToUInt32(fontId); workbookStylesPart.Stylesheet.CellFormats.Append(copy);
This all works fine, I can apply this cellformat to a cell and it is properly formatted, but as soon as I open try to add this to a CellStyle my Spreadsheet is broken and I can't open it anymore. This is how I add the CellStyle:
workbookStylesPart.Stylesheet.CellFormats.Append(copy); int cellFormatId = workbookStylesPart.Stylesheet.CellFormats.Count() - 1; CellStyle cellStyle2 = new CellStyle() { Name = "linkformat", FormatId = Convert.ToUInt32(cellFormatId), BuiltinId = (UInt32Value)0U };If I change the FormatId back to the value "1U" it works again. Does anyone have an idea how I can get this to work?