Note. The OpenXML 2.0 SDK is currently located in CTP and does not have a production license until Office2010.
My general methodology for working with the OpenXML SDK is to create an empty document and a document using only those functions that you would like to learn how to implement (for example, the background color) and use the OpenXmlDiff SDK to find out what changes should be in order to implement this function.
If you are creating a document from scratch, you can use the DocumentReflector to generate code for the default Stylesheet object, and then add the styles you want.
Starting from the default value:
new Stylesheet( new Fonts( new Font( new FontSize() { Val = 10D }, new Color() { Theme = (UInt32Value)1U }, new FontName() { Val = "Arial" }, new FontFamilyNumbering() { Val = 2 }) ) { Count = (UInt32Value)1U }, new Fills( new Fill( new PatternFill() { PatternType = PatternValues.None }), new Fill( new PatternFill() { PatternType = PatternValues.Gray125 }) ) { Count = (UInt32Value)2U }, new Borders(... ... ... new CellFormats( new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U }) { Count = (UInt32Value)1U }, ...
I added a new font of size 12 and a new Fill with a red background (indexed value of 64) and added new CellFormats that refer to the index of the new font and fill. (Be sure to update the counters)
new Stylesheet( new Fonts( new Font( new FontSize() { Val = 10D }, new Color() { Theme = (UInt32Value)1U }, new FontName() { Val = "Arial" }, new FontFamilyNumbering() { Val = 2 }), new Font( new FontSize() { Val = 12D }, new Color() { Theme = (UInt32Value)1U }, new FontName() { Val = "Arial" }, new FontFamilyNumbering() { Val = 2 }) ) { Count = (UInt32Value)2U }, new Fills( new Fill( new PatternFill() { PatternType = PatternValues.None }), new Fill( new PatternFill() { PatternType = PatternValues.Gray125 }), new Fill( new PatternFill() { PatternType = PatternValues.Solid, ForegroundColor = new ForegroundColor() { Rgb = "FFFF0000" }, BackgroundColor = new BackgroundColor() { Indexed = 64 } }) ) { Count = (UInt32Value)3U }, new Borders( new Border( new LeftBorder(), new RightBorder(), new TopBorder(), new BottomBorder(), new DiagonalBorder()) ) { Count = (UInt32Value)1U }, new CellStyleFormats( new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U } ) { Count = (UInt32Value)1U }, new CellFormats( new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U }, new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)1U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U }, new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)2U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U } ) { Count = (UInt32Value)3U }, new CellStyles( new CellStyle() { Name = "Normal", FormatId = (UInt32Value)0U, BuiltinId = (UInt32Value)0U } ) { Count = (UInt32Value)1U }, new DifferentialFormats() { Count = (UInt32Value)0U }, new TableStyles() { Count = (UInt32Value)0U, DefaultTableStyle = "TableStyleMedium9", DefaultPivotStyle = "PivotStyleLight16" });
Then in the code I apply the CellStyle index to the cells that I want to format: (There was already data in cells A2 and A3. Cell A2 gets a larger size, A3 gets a red background)
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>(); sheetData.Descendants<Row>().Where(r => r.RowIndex == 2U).First().Descendants<Cell>().First().StyleIndex = 1U; sheetData.Descendants<Row>().Where(r => r.RowIndex == 3U).First().Descendants<Cell>().First().StyleIndex = 2U;