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)
如果您在 Excel 颜色下拉列表中选择了一种“主题颜色”而不是“标准颜色”,或者从颜色选择器中选择了其中一种,则它似乎不起作用,如此处对这个问题的回答中所述:EPPlus Excel 更改单元格颜色
似乎不支持主题 - EPPlus 常见问题
库不支持什么(这些是最明显的功能)?[...] * 主题