title: Cell styles in OpenXML spreadsheet (SpreadsheetML)
body: <p>I've generated a .xlsx spreadsheet in C# using the OpenXML SDK, but can't figure out how to get cell styles working. I've been studying files produced by Excel, and can't quite figure out how it's done.</p>
  
  <p>Right now, I'm creating a fill, creating a <code>CellStyleFormat</code> that points at the fill, creating a <code>CellFormat</code> that points at the index of the <code>CellStyleFormat</code>, then creating a <code>CellStyle</code> that points to the <code>CellFormat</code>.</p>
  
  <p>Here's the code I'm using to generate the document:</p>
  
  <pre><code>Console.WriteLine("Creating document");
  using (var spreadsheet = SpreadsheetDocument.Create("output.xlsx", SpreadsheetDocumentType.Workbook))
  {
      Console.WriteLine("Creating workbook");
      spreadsheet.AddWorkbookPart();
      spreadsheet.WorkbookPart.Workbook = new Workbook();
      Console.WriteLine("Creating worksheet");
      var wsPart = spreadsheet.WorkbookPart.AddNewPart&lt;WorksheetPart&gt;();
      wsPart.Worksheet = new Worksheet();
  
      var stylesPart = spreadsheet.WorkbookPart.AddNewPart&lt;WorkbookStylesPart&gt;();
      stylesPart.Stylesheet = new Stylesheet();
      stylesPart.Stylesheet.Fills = new Fills();
  
      // create a solid red fill
      var solidRed = new PatternFill() { PatternType = PatternValues.Solid };
      solidRed.AppendChild(new BackgroundColor { Rgb = HexBinaryValue.FromString("FF00FF00") });
  
      stylesPart.Stylesheet.Fills.AppendChild(new Fill { PatternFill = new PatternFill() { PatternType = PatternValues.None } });
      stylesPart.Stylesheet.Fills.AppendChild(new Fill { PatternFill = solidRed });
      stylesPart.Stylesheet.CellStyleFormats = new CellStyleFormats();
      stylesPart.Stylesheet.CellStyleFormats.AppendChild(new CellFormat { FillId = 0, ApplyFill = false });
      stylesPart.Stylesheet.CellStyleFormats.AppendChild(new CellFormat { FillId = 1, ApplyFill = true });
      stylesPart.Stylesheet.CellFormats = new CellFormats();
      stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat { FormatId = 0 });
      stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat { FormatId = 1 });
      stylesPart.Stylesheet.CellStyles = new CellStyles();
      stylesPart.Stylesheet.CellStyles.AppendChild(new CellStyle { Name = "None", FormatId = 0 });
      stylesPart.Stylesheet.CellStyles.AppendChild(new CellStyle { Name = "Solid Red", FormatId = 1 });
  
      stylesPart.Stylesheet.Save();
  
      Console.WriteLine("Creating sheet data");
      var sheetData = wsPart.Worksheet.AppendChild(new SheetData());
  
      Console.WriteLine("Adding rows / cells...");
  
      var row = sheetData.AppendChild(new Row());
      row.AppendChild(new Cell() { CellValue = new CellValue("This"),  DataType = CellValues.String });
      row.AppendChild(new Cell() { CellValue = new CellValue("is"),    DataType = CellValues.String });
      row.AppendChild(new Cell() { CellValue = new CellValue("a"),     DataType = CellValues.String });
      row.AppendChild(new Cell() { CellValue = new CellValue("test."), DataType = CellValues.String });
  
      sheetData.AppendChild(new Row());
  
      row = sheetData.AppendChild(new Row());
      row.AppendChild(new Cell() { CellValue = new CellValue("Value:"),   DataType = CellValues.String });
      row.AppendChild(new Cell() { CellValue = new CellValue("123"),      DataType = CellValues.Number });
      row.AppendChild(new Cell() { CellValue = new CellValue("Formula:"), DataType = CellValues.String });
      row.AppendChild(new Cell() { CellFormula = new CellFormula("B3"),   StyleIndex = 1 }); // 
  
      Console.WriteLine("Saving worksheet");
      wsPart.Worksheet.Save();
  
      Console.WriteLine("Creating sheet list");
      var sheets = spreadsheet.WorkbookPart.Workbook.AppendChild(new Sheets());
      sheets.AppendChild(new Sheet() { Id = spreadsheet.WorkbookPart.GetIdOfPart(wsPart), SheetId = 1, Name = "Test" });
  
      Console.WriteLine("Saving workbook");
      spreadsheet.WorkbookPart.Workbook.Save();
  
      Console.WriteLine("Done.");
  }
  </code></pre>
  
  <p>Here's the generated XML:</p>
  
  <p><strong>workbook.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;x:workbook xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"&gt;
    &lt;x:sheets&gt;
      &lt;x:sheet name="Test" sheetId="1" r:id="Rbad86b8c80844a16" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" /&gt;
    &lt;/x:sheets&gt;
  &lt;/x:workbook&gt;
  </code></pre>
  
  <p><strong>styles.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"&gt;
    &lt;x:fills&gt;
      &lt;x:fill&gt;
        &lt;x:patternFill patternType="none" /&gt;
      &lt;/x:fill&gt;
      &lt;x:fill&gt;
        &lt;x:patternFill patternType="solid"&gt;
          &lt;x:bgColor rgb="FF00FF00" /&gt;
        &lt;/x:patternFill&gt;
      &lt;/x:fill&gt;
    &lt;/x:fills&gt;
    &lt;x:cellStyleXfs&gt;
      &lt;x:xf fillId="0" applyFill="0" /&gt;
      &lt;x:xf fillId="1" applyFill="1" /&gt;
    &lt;/x:cellStyleXfs&gt;
    &lt;x:cellXfs&gt;
      &lt;x:xf xfId="0" /&gt;
      &lt;x:xf xfId="1" /&gt;
    &lt;/x:cellXfs&gt;
    &lt;x:cellStyles&gt;
      &lt;x:cellStyle name="None" xfId="0" /&gt;
      &lt;x:cellStyle name="Solid Red" xfId="1" /&gt;
    &lt;/x:cellStyles&gt;
  &lt;/x:styleSheet&gt;
  </code></pre>
  
  <p><strong>worksheets/sheet.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;x:worksheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"&gt;
    &lt;x:sheetData&gt;
      &lt;x:row&gt;
        &lt;x:c t="str"&gt;&lt;x:v&gt;This&lt;/x:v&gt;&lt;/x:c&gt;
        &lt;x:c t="str"&gt;&lt;x:v&gt;is&lt;/x:v&gt;&lt;/x:c&gt;
        &lt;x:c t="str"&gt;&lt;x:v&gt;a&lt;/x:v&gt;&lt;/x:c&gt;
        &lt;x:c t="str"&gt;&lt;x:v&gt;test.&lt;/x:v&gt;&lt;/x:c&gt;
      &lt;/x:row&gt;
      &lt;x:row /&gt;
      &lt;x:row&gt;
        &lt;x:c t="str"&gt;&lt;x:v&gt;Value:&lt;/x:v&gt;&lt;/x:c&gt;
        &lt;x:c t="n"&gt;&lt;x:v&gt;123&lt;/x:v&gt;&lt;/x:c&gt;
        &lt;x:c t="str"&gt;&lt;x:v&gt;Formula:&lt;/x:v&gt;&lt;/x:c&gt;
        &lt;x:c s="1"&gt;&lt;x:f&gt;B3&lt;/x:f&gt;&lt;/x:c&gt;
      &lt;/x:row&gt;
    &lt;/x:sheetData&gt;
  &lt;/x:worksheet&gt;
  </code></pre>
  
  <p>The last cell of the last row is where I'm trying to add the style.</p>
  
  <p>This all validates properly when I run it through the OpenXML SDK Productivity Tool, but, when I attempt to open the file in Excel, I get the following error:</p>
  
  <blockquote>
    <p>Repaired Records: Format from /xl/styles.xml part (Styles)</p>
  </blockquote>
  
  <p>The spreadsheet then shows, but the fill isn't applied.</p>
  
  <p>Any idea how to go about fixing this?</p>