Epplus获得正确的细胞背景rgb颜色

Ale*_*iro 5 .net c# excel epplus

我无法使用EPPLUS获取背景颜色的真实RGB值.

我的代码只适用于在excel上设置为RGB的颜色,具有托盘颜色的单元格无法识别.

这是代码,希望有人可以帮助我:

ExcelRangeBase c = sheet.Cells[k, j];
var wbs = sheet.Workbook.Styles;
var fill = c.Style.Fill;
string rgb = "";
if (fill.PatternType == OfficeOpenXml.Style.ExcelFillStyle.Solid)
{
  rgb = !String.IsNullOrEmpty(fill.BackgroundColor.Rgb) ? fill.BackgroundColor.Rgb :
  fill.PatternColor.LookupColor(fill.BackgroundColor);
}
else if (fill.PatternType != OfficeOpenXml.Style.ExcelFillStyle.None)
{
  rgb = !String.IsNullOrEmpty(fill.PatternColor.Rgb) ? fill.PatternColor.Rgb :
  fill.PatternColor.LookupColor(fill.PatternColor);
}
if (rgb.StartsWith("#")) rgb.Replace("#", "");
rgb = rgb.Trim();

// Removes ALPHA from ARGB
if (rgb.Length == 8 || rgb.Length == 5) rgb = rgb.Substring(2);
else if (rgb.Length > 8) rgb = rgb.Substring(rgb.Length - 6);

if (!rgb.StartsWith("#")) rgb = "#" + rgb;

string bg = "";
// I got this validation because most times lookupColor returns FF000;
if (rgb != null && rgb != "" && rgb != "#000" && rgb != "#000000")
{
  bg = "background: " + rgb + "; ";
}
Run Code Online (Sandbox Code Playgroud)

Bun*_*ity 5

如果您在 Excel 颜色下拉列表中选择了一种“主题颜色”而不是“标准颜色”,或者从颜色选择器中选择了其中一种,则它似乎不起作用,如此处对这个问题的回答中所述:EPPlus Excel 更改单元格颜色

似乎不支持主题 - EPPlus 常见问题

库不支持什么(这些是最明显的功能)?[...] * 主题

  • 感谢邦克的回复。我知道不支持主题,我认为我没有使用它们,我使用了 excel 中的默认颜色。我猜它们在 EPPlus 中被称为 Tinted 或 Indexed,我想我可以得到它们,因为 ExcelColor.LookupColor 方法说:“返回使用 Indexed 或 Tint 属性的颜色对象的 RGB 值”。我检查了一些单元格上的索引和色调属性,我的代码返回 #FF000 颜色 (2认同)