Próbuję pobrać kolor tła komórki w arkuszu kalkulacyjnym Excel. Używam Open XML 2.0 SDK i jestem w stanie otworzyć plik * .xlsx i na przykład uzyskać wartości komórek. Moje kodu dla uzyskania background-color jest następujący:Uzyskiwanie koloru tła komórki w programie Excel przy użyciu Open XML 2.0
public BackgroundColor GetCellBackColor(Cell theCell, SpreadsheetDocument document)
{
BackgroundColor backGroundColor = null;
WorkbookStylesPart styles = SpreadsheetReader.GetWorkbookStyles(document);
int cellStyleIndex = (int)theCell.StyleIndex.Value;
CellFormat cellFormat = (CellFormat)styles.Stylesheet.CellFormats.ChildElements[cellStyleIndex];
Fill fill = (Fill)styles.Stylesheet.Fills.ChildElements[(int)cellFormat.FillId.Value];
backGroundColor = fill.PatternFill.BackgroundColor;
return backGroundColor;
}
mój problem polega na tym, że PatternFill.BackgroundColor
powraca właśnie liczbą naturalną, myślę, że jest to id stylu. Moim problemem jest to, że linia kodu
DocumentFormat.OpenXml.Spreadsheet.Color c = (DocumentFormat.OpenXml.Spreadsheet.Color)styles.Stylesheet.Colors.ChildElements[Int32.Parse(backGroundColor.InnerText)];
wraca z błędem, ponieważ jest null
... Stylesheet.Colors
... może to dlatego, że stosowany jest „wbudowany” w kolorze Excel - nie jest zdefiniowany własny kolor?!
Jakieś pomysły, w jaki sposób mogę "obliczyć" rzeczywistą liczbę kolorów z "wartości backGroundColor"?
Klasa SpreadsheetReader nie istnieje w OPENXML 2,5 – Elmue