user2622016
user2622016

Reputation: 6413

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

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?

Upvotes: 4

Views: 1186

Answers (1)

user2997302
user2997302

Reputation: 115

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

sheetFormatting.addConditionalFormatting(newRange,arrayOfRules);

Upvotes: 3

Related Questions