Example usage for org.apache.poi.ss.usermodel SheetConditionalFormatting removeConditionalFormatting

List of usage examples for org.apache.poi.ss.usermodel SheetConditionalFormatting removeConditionalFormatting

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel SheetConditionalFormatting removeConditionalFormatting.

Prototype

void removeConditionalFormatting(int index);

Source Link

Document

Removes a Conditional Formatting object by index

Usage

From source file:de.jlo.talendcomp.excel.SpreadsheetOutput.java

License:Apache License

public void extendCellRangesForConditionalFormattings() throws Exception {
    if (workbook instanceof SXSSFWorkbook) {
        warn("Cannot extend cell ranges for conditional formats in the memory the saving mode (use of the streaming-workbook).");
    } else {//ww w  .j  a va 2s.  c  o m
        int firstDataRowIndex = firstRowIsHeader ? rowStartIndex + 1 : rowStartIndex;
        info("Extending cell ranges for conditional formats. Use formats from row: " + firstDataRowIndex);
        if (getLastRowNum() > 0 && getLastRowNum() > firstDataRowIndex) {
            SheetConditionalFormatting scf = sheet.getSheetConditionalFormatting();
            if (debug) {
                debug("#### Conditional formattings before:");
                debug(logoutSheetConditionalFormatting(scf));
            }
            ConditionalFormatting lastCf = null;
            for (Integer cellColumnIndex : usedCellColumnIndexes) {
                if (debug) {
                    debug("extendCellRangesForConditionalFormattings: check format for cell index="
                            + cellColumnIndex);
                }
                find(scf, firstDataRowIndex, cellColumnIndex); // currentCf and currentCfIndex will be set here
                if (currentCf != null && currentCf != lastCf) {
                    if (debug) {
                        debug("extendCellRangesForConditionalFormattings: found format for cell index="
                                + cellColumnIndex);
                    }
                    lastCf = currentCf;
                    CellRangeAddress[] ranges = currentCf.getFormattingRanges();
                    for (int i = 0; i < ranges.length; i++) {
                        CellRangeAddress address = ranges[i];
                        CellRangeAddress extendedAddress = new CellRangeAddress(address.getFirstRow(),
                                getLastRowNum(), address.getFirstColumn(), address.getLastColumn());
                        ranges[i] = extendedAddress;
                    }
                    if (debug) {
                        debug("extendCellRangesForConditionalFormattings: extend ranges to=" + firstDataRowIndex
                                + ":" + getLastRowNum() + " -> " + getRangesAsString(ranges));
                    }
                    int numRulesTotal = currentCf.getNumberOfRules();
                    if (numRulesTotal > 0) {
                        int chunks = numRulesTotal / maxRuleChunkSize;
                        int restChunkSize = numRulesTotal % maxRuleChunkSize;
                        int currentSize = 0;
                        for (int c = 0; c <= chunks; c++) {
                            if (c < chunks) {
                                // all not-last chunks have the max chunk size
                                currentSize = maxRuleChunkSize;
                            } else {
                                // the last chunk contains the rest
                                currentSize = restChunkSize;
                            }
                            if (currentSize > 0) {
                                ConditionalFormattingRule[] rules = new ConditionalFormattingRule[currentSize];
                                for (int i = 0; i < currentSize; i++) {
                                    int ruleIndex = i + (maxRuleChunkSize * c); // current pointer within a chunk + chunk offset
                                    rules[i] = currentCf.getRule(ruleIndex);
                                    if (debug) {
                                        debug("extendCellRangesForConditionalFormattings: add ranges: "
                                                + getRangesAsString(ranges) + " rule #" + ruleIndex + " ="
                                                + describeRule(rules[i]));
                                    }
                                }
                                scf.addConditionalFormatting(ranges, rules);
                            }
                        }
                        if (debug) {
                            debug("extendCellRangesForConditionalFormattings: remove template format at index:"
                                    + currentCfIndex);
                        }
                        scf.removeConditionalFormatting(currentCfIndex);
                    }
                }
            }
            if (debug) {
                debug("#### Conditional formattings after:");
                debug(logoutSheetConditionalFormatting(scf));
            }
        }
    }
}