I have a datatable in my .net core project. I want to color certain column according to the content. I created the datatable and then parsed the data.
DataTable dt = new();
dt.Columns.Add("Status");
dt.Columns.Add("Base");
dt.Columns.Add("First Date");
dt.Columns.Add("Second Date");
dt.Columns.Add("Failure Mode");
dataAll.ForEach(row =>
{
dt.Rows.Add(
row.status,
row.partBase,
row.pcaDate,
row.icaDate,
row.failureMode
);
});
I want to change the color of the status column. For example to color this cell red if the status is equal to R. How can I color the Datatable cell.This is how I export with Excel;
using XLWorkbook wb = new();
// create data table
DataTable dt = MapDataToExcel(response.Result.ToList());
// add as worksheet
wb.Worksheets.Add(dt, "Rapor");
// save it as a file then stream it to the user
using MemoryStream stream = new MemoryStream();
wb.SaveAs(stream);
return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Rapor.xlsx");
for (int i = 2; i < dataCount; i++)
{
foreach (var item in wb.Worksheets)
{
var objPage2 = item.Cell(i, 1).GetString();
if (objPage2 == "R") { item.Cell(i, 1).Style.Fill.BackgroundColor = XLColor.Red; }
if (objPage2 == "G") { item.Cell(i, 1).Style.Fill.BackgroundColor = XLColor.Green; }
if (objPage2 == "Y") { item.Cell(i, 1).Style.Fill.BackgroundColor = XLColor.Yellow; }
}
}
Check the value of the specific column and color them depending on value