【问题标题】:Epplus get correct cell background rgb colorEpplus 获得正确的单元格背景 rgb 颜色
【发布时间】:2019-05-28 17:10:29
【问题描述】:

我无法使用 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 + "; ";
}

【问题讨论】:

    标签: c# .net excel epplus


    【解决方案1】:

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

    好像不支持主题-EPPlus FAQ

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

    【讨论】:

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

    事实证明,这是LookupColor 在 EPPlus 中的工作方式的一个怪癖。具体来说,颜色returned in this case is AA{R}{G}{B} 的格式,两个字符表示alpha,R、G 和B 分别有一个恒定长度序列,指定灰色阴影。但是,如果您查看代码,您可能会得到一些非常奇怪的颜色(即它可能被窃听)。这是因为所使用的常量长度可以是 1 到 3 个字符,上限为 0x0200

    例如,((int)(decimal.Round(-1M * -512))).ToString("X") 返回 "200",根据推断,这将导致返回 #FF200200200。但是,如果没有提交补丁来更改处理方式,可能的方法是相信 可以为频道返回的上限,然后在 0-> 之间缩放法。

    有关执行此操作的方法,请参见下文。请注意,如果这个 曾经在 EPPlus 本身中得到修复,则以下内容会错误地缩放(因为实际的上限将是 FF,而不是 0x0200)。

    private string EPPLookupColorFixed(ExcelColor sourceColor)
        {
            var lookupColor = sourceColor.LookupColor();
            const int maxLookup = 63;
            bool isFromTable = (0 <= sourceColor.Indexed) && (maxLookup > sourceColor.Indexed);
            bool isFromRGB = (null != sourceColor.Rgb && 0 < sourceColor.Rgb.Length);
            if (isFromTable || isFromRGB)
                return lookupColor;
    
            // Ok, we know we entered the else block in EPP - the one 
            // that doesn't quite behave as expected.
    
            string shortString = "0000";
            switch (lookupColor.Length)
            {
                case 6:
                    // Of the form #FF000
                    shortString = lookupColor.Substring(3, 1).PadLeft(4, '0');
                    break;
                case 9:
                    // Of the form #FFAAAAAA
                    shortString = lookupColor.Substring(3, 2).PadLeft(4, '0');
                    break;
                case 12:
                    // Of the form #FF200200200
                    shortString = lookupColor.Substring(3, 3).PadLeft(4, '0');
                    break;
            }
            var actualValue = short.Parse(shortString, System.Globalization.NumberStyles.HexNumber);
            var percent = ((double)actualValue) / 0x200d;
            var byteValue = (byte)Math.Round(percent * 0xFF,0);
            var byteText = byteValue.ToString("X");
            byteText = byteText.Length == 2 ? byteText : byteText.PadLeft(2, '0');
            return $"{lookupColor.Substring(0, 3)}{byteText}{byteText}{byteText}";
        }
    

    【讨论】:

    • 是的,但是 LookupColor 对任何主题颜色都没有帮助 - EPPlus 不支持主题,并且在不知道应用于工作表的主题范围的情况下,您无法辨别色调对应的颜色'再看属于。
    猜你喜欢
    • 2015-10-21
    • 2015-10-12
    • 2017-03-26
    • 2021-11-20
    • 1970-01-01
    • 2022-12-12
    • 2015-11-29
    • 1970-01-01
    • 2013-11-07
    相关资源
    最近更新 更多