我正在使用Epplus导出到Excel。使用C#中的EPPLus根据文本设置单元格的条件背景颜色.net
我想根据第1列值设置第1列和第2列的背景颜色。如果第2列单元格中的任何单元格包含1,则col1和col2的背景颜色为绿色。如果它包含2,那么背景颜色必须是浅黄色。像下面的图片。
现在我只能设置第二列的背景颜色。如果我设置了范围,那么它会根据Last条件设置背景颜色,并将整个列颜色设置为黄色。请帮助我。
我正在使用Epplus导出到Excel。使用C#中的EPPLus根据文本设置单元格的条件背景颜色.net
我想根据第1列值设置第1列和第2列的背景颜色。如果第2列单元格中的任何单元格包含1,则col1和col2的背景颜色为绿色。如果它包含2,那么背景颜色必须是浅黄色。像下面的图片。
现在我只能设置第二列的背景颜色。如果我设置了范围,那么它会根据Last条件设置背景颜色,并将整个列颜色设置为黄色。请帮助我。
选择Col1和Col2中的所有数据。转到条件格式菜单并点击管理规则。选择列表中的最后一个选项(使用公式来确定...)并使用以下公式: = if($ [col2] [row1ofData] = 1,true,false)。然后按照你的意愿格式化。将规则应用于Col1和Col2。
尽管将规则应用于Col1,美元符号会告诉它查看Col2中的值,以查找范围中的任何行。
然后,您必须对您想要使用的每种颜色代码重复此操作(即,一次将颜色1变为绿色,再将颜色2变为琥珀色)。
我想用EPPlUS来做这个,而不是用MS Excel – Richa
然后我建议你澄清一下你的问题,因为很难知道你需要哪些软件来帮助。请注意,你用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);
}
}
可能要发布您目前使用的代码。 – Ernie
您是否找到了解决方案? – Richa
发布迄今为止的代码,以便其他人可以帮助查看正在发生的事情。 – Ernie