Read Excel cell Background-Color from Excel Sheet using open Xml SDK
//Read background color and fill the pattern
DocumentFormat.OpenXml.Spreadsheet.PatternFill pf = GetCellPatternFill(cell, doc);
DocumentFormat.OpenXml.Spreadsheet.ColorType ct= pf.ForegroundColor;
string backgroundcolor=ct.Rgb.Value;
public static DocumentFormat.OpenXml.Spreadsheet.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.Spreadsheet.Fill fill = (DocumentFormat.OpenXml.Spreadsheet.Fill)styles.Stylesheet.Fills.ChildElements[(int)cellFormat.FillId.Value];
return fill.PatternFill;
}
No comments:
Post a Comment