我正在使用Epplus导出到Excel。
我想根据第1列值设置第1列和第2列的背景颜色。如果第2列单元格中的任何单元格包含1,则col1和col2的背景颜色为绿色。如果它包含2,那么背景颜色必须是浅黄色。如下图所示。
现在我只能设置第二列背景颜色。如果我设置了范围,那么它会根据Last条件设置背景颜色,并将整个列的颜色设置为黄色。请帮帮我。
答案 0 :(得分:0)
选择Col1和Col2中的所有数据。转到条件格式菜单,然后单击管理规则。选择列表中的最后一个选项(使用公式确定...)并使用以下公式: = IF(的 $ 强> [COL2] [row1ofData] = 1,TRUE,FALSE)。然后按您的意愿格式化。将规则应用于Col1和Col2。
美元符号将告诉它查看Col2中的值,尽管将规则应用于Col1,范围内的任何行。
然后,您必须为要使用的每种颜色代码重复此操作(即,将颜色1作为绿色进行一次,再将颜色2再颜色为琥珀色)。
答案 1 :(得分:0)
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);
}
}