So I was trying to replicate this answer Getting cell-backgroundcolor in Excel with Open XML 2.0
but VS is complaining about SpreadsheetReader
not existing in the current context. I can't ctrl+.
resolve the issue.
Here is the (almost working) code:
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;
public static DocumentFormat.OpenXml.Drawing.PatternFill GetCellPatternFill(Cell theCell, SpreadsheetDocument document)
{
WorkbookStylesPart styles = SpreadsheetReader.GetWorkbookStyles(document);
int cellStyleIndex;
if (theCell.StyleIndex == null) // I think (from testing) if the StyleIndex is null
{ // then this means use cell style index 0.
cellStyleIndex = 0; // However I did not found it in the open xml
} // specification.
else
{
cellStyleIndex = (int)theCell.StyleIndex.Value;
}
CellFormat cellFormat = (CellFormat)styles.Stylesheet.CellFormats.ChildElements[cellStyleIndex];
DocumentFormat.OpenXml.Drawing.Fill fill = (DocumentFormat.OpenXml.Drawing.Fill)styles.Stylesheet.Fills.ChildElements[(int)cellFormat.FillId.Value];
return fill.PatternFill;
}