首页 文章

使用C#.net中的EPPLus基于文本设置单元格的条件背景颜色

提问于
浏览
0

我正在使用Epplus导出到Excel .

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

现在我只能设置第二列背景颜色 . 如果我设置了范围,那么它会根据Last条件设置背景颜色,并将整个列的颜色设置为黄色 . 请帮帮我 .
enter image description here

2 回答

  • 0

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

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

    然后,您必须为要使用的每种颜色代码重复此操作(即,将颜色1作为绿色再进行一次,然后将颜色2再次作为琥珀色) .

  • 0

    我找到了自己的解决方案 . 下面是excel输出
    enter image description here
    .

    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);
            }
        }
    

相关问题