Skip to content
Advertisement

Apache POI – Conditional formatting – need to set different cell range for rule and formatting

I am trying to create an empty excel template using apache poi java. I need to add a rule – when column no. 3 is populated then columns from 7 to 12 need to be highlighted in some color (as a mandatory indicator for the user).

I could find below code which colors the cell when the condition is met on the same cell. But I want to color/format different cells when the condition is met on current cell.

JavaScript

But I want is when rule1 is met, then color a different cell range.

Is this possible in poi and how ?

Advertisement

Answer

Excel provides conditional formatting rules based on formulas.

The formula =AND(ISNUMBER($C1), $C1>5) returns True if the value in $C1 is numeric and greater than 5. If this formula is applied to the range G1:L1000, then each cell in this range will be true if the value in column C of the corresponding row fulfills that contition. That is because column C is fixated using $C in the formula. But the row numbers are not fixated and so are relative.

Example using apache poi:

JavaScript

Now if you put something in column C what is numeric and greater than 5, the cells in columns G:L will be filled yellow.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement