代码之家  ›  专栏  ›  技术社区  ›  sfaust

EPPlus合并背景色?

  •  0
  • sfaust  · 技术社区  · 7 年前

    我正在使用ePlus在Excel表格中填写一些数据。作为这项工作的一部分,我将根据程序中的其他数据设置背景色。这似乎是可行的,但是颜色出来比我放进去的颜色更深。我把它弄得一团糟,我想如果我把它们放在模板中白色的一行中,它们会显示正确,但如果我把它们放在背景设置为灰色的一行中,它们会显示为黑色。似乎ePlus(或Excel)正在将颜色合并在一起。。。

    我希望行整体为灰色,但希望能够在某些位置为特定单元格设置颜色替代。这是我的代码:

                    Color fillColor;
                    Color foreColor = Color.Black;
    
                    if (pd.BuiltInParameter == default(int))
                    {
                        fillColor = Color.Black;
                        foreColor = Color.White;
                    }
                    else
                    {
                        switch (pd.ParameterRevitType)
                        {
                            case "Text":
                                fillColor = Color.FromArgb(198, 89, 17);
                                break;
                            case "URL":
                                fillColor = Color.FromArgb(68, 114, 196);
                                break;
                            case "Length":
                                fillColor = Color.FromArgb(255, 153, 51);
                                break;
                            case "Material":
                                fillColor = Color.FromArgb(48, 84, 150);
                                foreColor = Color.White;
                                break;
                            case "YesNo":
                                fillColor = Color.FromArgb(112, 48, 160);
                                foreColor = Color.White;
                                break;
                            case "FamilyType":
                                fillColor = Color.FromArgb(255, 255, 0);
                                break;
                            case "Image":
                                fillColor = Color.FromArgb(0, 176, 240);
                                break;
                            case "Currency":
                                fillColor = Color.FromArgb(0, 176, 80);
                                break;
                            default:
                                fillColor = Color.Red;
                                foreColor = Color.White;
                                break;
                        }
                    }
    
                    ws.Cells[4, currentColumn].Style.Fill.PatternType = ExcelFillStyle.Solid;
                    ws.Cells[4, currentColumn].Style.Fill.BackgroundColor.SetColor(fillColor);
                    if (foreColor != Color.Black)
                    {
                        ws.Cells[3, currentColumn].Style.Font.Color.SetColor(foreColor);
                    }
    

    有没有办法让它只使用指定的颜色?我错过了什么?

    以下是VS中的颜色预览图和显示的颜色:

    enter image description here

    作为测试,我让它设置了主行和下面的行(下面是白色)。结果如下:

    enter image description here

    更新

    好的,我有一个可复制的样品。下面是一个小型控制台应用程序的代码:

    class Program
    {
        static void Main(string[] args)
        {
            string folderPath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
            string templatePath = Path.Combine(folderPath, "Template.xlsx");
            string filePath = Path.Combine(folderPath, "Export.xlsx");
    
            if (File.Exists(filePath)) File.Delete(filePath);
            File.Copy(templatePath, filePath);
    
            using (var ep = new ExcelPackage(new FileInfo(filePath)))
            {
                var ws = ep.Workbook.Worksheets["Families-Types"];
                ws.Cells[3, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(150, 245, 125));
                ep.Save();
            }
        }
    }
    

    Here 是指向其引用的模板的链接,并且 Here 是指向我从程序中获得的导出的链接。起初看起来几乎正确,但导出的颜色比程序中的rgb颜色深绿色。如果转到Excel中的单元格格式并输入相同的rgb值,然后按“确定”,则可以看到这一点,它将显著变亮。

    1 回复  |  直到 7 年前
        1
  •  2
  •   sfaust    7 年前

    好的,我终于找到了问题。我又深入研究了xml,并能够在简化的示例中更好地理解它。模板中设置的原始颜色似乎有一个“tint”值(在本例中为-0.499984740745262,但根据颜色而不同)。这种色调会修改颜色,并且由于某种原因会延续到新颜色。幸运的是,ePlus有一个可设置的tint属性。我将其设置为0和“叮!”!“叮!”颜色正确。

    下面是工作应用程序的更新代码,请注意设置颜色后的额外一行:

    class Program
    {
        static void Main(string[] args)
        {
            string folderPath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
            string templatePath = Path.Combine(folderPath, "Template.xlsx");
            string filePath = Path.Combine(folderPath, "Export.xlsx");
    
            if (File.Exists(filePath)) File.Delete(filePath);
            File.Copy(templatePath, filePath);
    
            using (var ep = new ExcelPackage(new FileInfo(filePath)))
            {
                var ws = ep.Workbook.Worksheets["Families-Types"];
                ws.Cells[3, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(150, 245, 125));
                ws.Cells[3, 1].Style.Fill.BackgroundColor.Tint = 0;
                ep.Save();
            }
        }
    }
    

    希望这能帮助别人。