Rituparno Bhattacharya
Rituparno Bhattacharya

Reputation: 363

Set row color with closedXML based on column value

I am trying to write Excel file from datatable with VB.NET. I am able to achieve few excel operations also. Now I want to colour a row based on cell value of a particular column (Col-4 for example). Here is my Excel file -

Here is my Excel file

I want to achieve like this -

Looking some guidance to achieve this.

Upvotes: 1

Views: 7424

Answers (1)

Francois Botha
Francois Botha

Reputation: 4849

I suggest you add conditional formatting. For example:

var workbook = new XLWorkbook();
var ws = workbook.AddWorksheet("Sheet1");

ws.FirstCell().SetValue(1)
  .CellBelow().SetValue(1)
  .CellBelow().SetValue(2)
  .CellBelow().SetValue(3)
  .CellBelow().SetValue(4);

ws.RangeUsed().AddConditionalFormat().WhenBetween(2, 3)
  .Fill.SetBackgroundColor(XLColor.Red);

Reference: https://github.com/ClosedXML/ClosedXML/wiki/Conditional-Formatting

Upvotes: 3

Related Questions