Excel电子表格中单元格的填充模式是
由两种颜色组成:背景色和前景色。
术语“前景色”在这里有点误导。它不是
字体的颜色,但图案填充的前景色。
例如,如果您用纯色填充单元格的背景
这ForegroundColor
相关财产PatternFill
细胞的对象
设置为所选的纯色值,而BackgroundColor
目的
设置为系统前景色。这PatternType
的财产PatternFill
对象设置为PatternValues.Solid
.
因此,要获取单元格背景(实心填充)的颜色值,您必须分析
这ForegroundColor
相关财产PatternFill
目的。你必须
确定实例代表的“颜色类型”:
- 自动颜色和系统相关颜色
- 索引颜色。
- ARGB 颜色(阿尔法、红色、绿色和蓝色)
- 基于主题的颜色。
- 应用于颜色的色调值。
有关不同“颜色类型”的更多信息,请参阅以下内容link.
请注意以下的含义InnerText
的财产ForegroundColor
and BackgroundColor
类别取决于颜色类型。例如,如果是基于主题的颜色InnerText
财产
被设置为索引ColorScheme
收藏。
以下示例打印电子表格文档中所有单元格的所有背景颜色信息:
public static 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];
Fill fill = (Fill)styles.Stylesheet.Fills.ChildElements[(int)cellFormat.FillId.Value];
return fill.PatternFill;
}
private static void PrintColorType(SpreadsheetDocument sd, DocumentFormat.OpenXml.Spreadsheet.ColorType ct)
{
if (ct.Auto != null)
{
Console.Out.WriteLine("System auto color");
}
if (ct.Rgb != null)
{
Console.Out.WriteLine("RGB value -> {0}", ct.Rgb.Value);
}
if (ct.Indexed != null)
{
Console.Out.WriteLine("Indexed color -> {0}", ct.Indexed.Value);
//IndexedColors ic = (IndexedColors)styles.Stylesheet.Colors.IndexedColors.ChildElements[(int)bgc.Indexed.Value];
}
if (ct.Theme != null)
{
Console.Out.WriteLine("Theme -> {0}", ct.Theme.Value);
Color2Type c2t = (Color2Type)sd.WorkbookPart.ThemePart.Theme.ThemeElements.ColorScheme.ChildElements[(int)ct.Theme.Value];
Console.Out.WriteLine("RGB color model hex -> {0}", c2t.RgbColorModelHex.Val);
}
if (ct.Tint != null)
{
Console.Out.WriteLine("Tint value -> {0}", ct.Tint.Value);
}
}
static void ReadAllBackgroundColors()
{
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open("c:\\temp\\bgcolor.xlsx", false))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
foreach(WorksheetPart worksheetPart in workbookPart.WorksheetParts)
{
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
foreach (Row r in sheetData.Elements<Row>())
{
foreach (Cell c in r.Elements<Cell>())
{
Console.Out.WriteLine("----------------");
PatternFill pf = GetCellPatternFill(c, spreadsheetDocument);
Console.Out.WriteLine("Pattern fill type -> {0}", pf.PatternType.Value);
if (pf.PatternType == PatternValues.None)
{
Console.Out.WriteLine("No fill color specified");
continue;
}
Console.Out.WriteLine("Summary foreground color:");
PrintColorType(spreadsheetDocument, pf.ForegroundColor);
Console.Out.WriteLine("Summary background color:");
PrintColorType(spreadsheetDocument, pf.BackgroundColor);
}
}
}
}
}
static void Main(string[] args)
{
ReadAllBackgroundColors();
}