c# - Epplus 获取正确的单元格背景 rgb 颜色

标签 c# .net 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 + "; ";
}

最佳答案

如果您在 Excel 颜色下拉列表中选择了一种“主题颜色”而不是“标准颜色”,或者从颜色选择器中选择它似乎不起作用,如此处问题的答案所述:EPPlus Excel Change cell color

似乎不支持主题 - EPPlus FAQ

What is NOT supported by the library (these are the most obvious features)? [...] * Themes

关于c# - Epplus 获取正确的单元格背景 rgb 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35941241/

相关文章:

c# - Linq to XML 选择所有父项和子项

c# - 加载程序集及其依赖项

c# - 存储过程读取xml,有什么更好的办法吗? SQL 服务器 2008

excel - if语句在满足条件时不改变颜色

vba - 将搜索从一个单元格更改为整个工作表

c# WPF 对 Winform 控件的透明性

c# - 将 PrinterSettings 序列化为字符串或 blob 以插入数据库

.net - 使用 ASP.NET MVC 进行 jQuery 表单例份验证

c# - 将字典键从 json 反序列化为 .net 中的枚举

excel - 如何在 VBA 中自动填充当月的一列,然后自动填充下一列?