Home » excel » java – Apache POI, how to modify conditional formatting range in Excel file?

java – Apache POI, how to modify conditional formatting range in Excel file?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I need to resize range of conditional formatting.

Read an Excel file (.xlsm, .xlsx), modify ranges for conditional formatting, and save it as new file.

SheetConditionalFormatting sheetFormatting = sheet.getSheetConditionalFormatting();
for( int i = 0; i < sheetFormatting.getNumConditionalFormattings() ; i++ )
    for( CellRangeAddress region : 
            sheetFormatting.getConditionalFormattingAt(i).getFormattingRanges() )
        if( region.getFirstRow() == 1 )
            region.setLastRow( 1+data.length );

So I just get all conditional formatting objects, get range objects for them, and modify ranges by calling CellRangeAddress.setLastRow().

The problem is that this modification is not present in result file. In fact, there are no changes in sheet.getSheetConditionalFormatting() just after the operation.

Need I ‘commit’ somehow changes to ConditionalFormatting?

How to&Answers:

Yes, you should add Conditional Formatting to your sheet formatting.

sheetFormatting.addConditionalFormatting(newRange,arrayOfRules);