【问题标题】:Set conditional background color of cell based on text using EPPLus in C#.net在 C#.net 中使用 EPPLus 根据文本设置单元格的条件背景颜色
【发布时间】:2017-01-21 21:43:00
【问题描述】:

我正在使用 Epplus 导出到 Excel。

我想根据第 1 列的值设置第 1 列和第 2 列的背景颜色。如果第 2 列单元格中的任何单元格包含 1,则 col1 和 col2 的背景颜色为绿色。如果它包含 2 则背景颜色必须是浅黄色。如下图。

现在我只能设置第二列背景颜色。如果我设置范围,那么它会根据最后一个条件设置背景颜色,并将整个列着色为黄色。请帮帮我。

【问题讨论】:

  • 可能想发布您目前拥有的代码。
  • 你找到解决办法了吗?
  • 发布您目前拥有的代码,以便其他人可以帮助查看发生了什么。

标签: c# background-color conditional-formatting epplus


【解决方案1】:

选择 Col1 和 Col2 中的所有数据。转到条件格式菜单并单击管理规则。选择列表中的最后一个选项(使用公式确定...)并使用以下公式: =if($[col2][row1ofData]=1,true,false)。然后根据需要格式化。将规则应用于 Col1 和 Col2。

美元符号将告诉它查看 Col2 中的值,尽管将规则应用于 Col1,但对于范围内的任何行。

然后,您必须对要使用的每个颜色代码重复此操作(即,将 1 着色为绿色,然后将 2 着色为琥珀色)。

【讨论】:

  • 我想在 MS Excel 中使用 EPPlUS 来完成这项工作
  • 那么我建议您澄清您的问题,因为很难知道您需要哪些软件的帮助。请注意,您已用 excel 标记了您的问题,因此我的回答。
【解决方案2】:

我找到了自己的解决方案。下面是excel输出

 int Tocolumn = ws.Dimension.End.Column;

  foreach (ExcelRangeBase cell in ws.Cells[2, 1, ToRow, 2])
    {
        if (string.IsNullOrEmpty(cell.Text)) continue;
        var text = cell.Text;

        if (text.Equals("0"))
        {
            Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#7fcbfe");
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.BackgroundColor.SetColor(colFromHex);
        }
        else if (text.Equals("1"))
        {
            Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#90ee90");
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.BackgroundColor.SetColor(colFromHex);
        }
        else if (text.Equals("2"))
        {
            Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#ffee75");
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.BackgroundColor.SetColor(colFromHex);
        }
        else if (text.Equals("3"))
        {
            Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#fdb957");
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.BackgroundColor.SetColor(colFromHex);
        }
        else if (text.Equals("4"))
        {
            Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#FF9985");
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.BackgroundColor.SetColor(colFromHex);
        }
        else if (text.Equals("5"))
        {
            Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#33CCCC");
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.BackgroundColor.SetColor(colFromHex);
        }
        else if (text.Equals("6"))
        {
            Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#66CCFF");
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.BackgroundColor.SetColor(colFromHex);
        }
        else if (text.Equals("7"))
        {
            Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#FFFF99");
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
            ws.Cells[cell.Start.Row, cell.Start.Column - 1, cell.Start.Row, cell.Start.Column].Style.Fill.BackgroundColor.SetColor(colFromHex);
        }
    }

【讨论】:

    猜你喜欢
    • 2019-10-11
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 2017-03-26
    • 2022-01-20
    相关资源
    最近更新 更多