title: Change Sheet tab color of excel file using Open XML
body: <p>I want to change the sheet tab color of an excel Xlsx  document. I am using the following code but it does not set the sheet color. I get object reference exception when I set the sheet tab color.</p>
  
  <pre><code>public static string filepath = @"C:\Test\Book1.xlsx";
  private static void ChangeSheetcolor()
  {
      try
      {
          using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(filepath, false))
          {
              WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
              IEnumerable&lt;Sheet&gt; sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild&lt;Sheets&gt;().Elements&lt;Sheet&gt;();
              //my code
              WorksheetPart worksheetPart =
                    GetWorksheetPartByName(spreadSheetDocument, "Sheet1");
  
              if (worksheetPart != null)
              {
               //   worksheetPart.Worksheet.SheetProperties.TabColor.Rgb = DocumentFormat.OpenXml.HexBinaryValue.FromString("Red");
                  worksheetPart.Worksheet.SheetProperties.TabColor.Rgb = DocumentFormat.OpenXml.HexBinaryValue.FromString("#CCCCCC");
                  // Save the worksheet.
                  worksheetPart.Worksheet.Save();
              }
          }
      }
      catch (Exception ex)
      { 
  
      }            
  }
  private static WorksheetPart
       GetWorksheetPartByName(SpreadsheetDocument document,
       string sheetName)
  {
      IEnumerable&lt;Sheet&gt; sheets =
         document.WorkbookPart.Workbook.GetFirstChild&lt;Sheets&gt;().
         Elements&lt;Sheet&gt;().Where(s =&gt; s.Name == sheetName);
      if (sheets.Count() == 0)
      {
          //does not exist
          return null;
      }
      string relationshipId = sheets.First().Id.Value;
      WorksheetPart worksheetPart = (WorksheetPart)
           document.WorkbookPart.GetPartById(relationshipId);
      return worksheetPart;
  }
  </code></pre>
  
  <p>How to change the sheet tab color using Open XML dlls?</p>