Example usage for org.apache.poi.ss.usermodel ConditionalFormattingRule getFontFormatting

List of usage examples for org.apache.poi.ss.usermodel ConditionalFormattingRule getFontFormatting

Introduction

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

Prototype

FontFormatting getFontFormatting();

Source Link

Usage

From source file:com.vaadin.addon.spreadsheet.ConditionalFormatter.java

/**
 * Creates the necessary CSS rules and runs evaluations on all affected
 * cells./*from   w  w  w  .  j a va  2  s. c om*/
 */
public void createConditionalFormatterRules() {

    // make sure old styles are cleared
    if (cellToIndex != null) {
        for (String key : cellToIndex.keySet()) {
            int col = SpreadsheetUtil.getColumnIndexFromKey(key) - 1;
            int row = SpreadsheetUtil.getRowFromKey(key) - 1;
            Cell cell = spreadsheet.getCell(row, col);
            if (cell != null) {
                spreadsheet.markCellAsUpdated(cell, true);
            }
        }
    }

    cellToIndex.clear();
    topBorders.clear();
    leftBorders.clear();
    spreadsheet.getState().conditionalFormattingStyles = new HashMap<Integer, String>();

    SheetConditionalFormatting cfs = spreadsheet.getActiveSheet().getSheetConditionalFormatting();

    if (cfs instanceof HSSFSheetConditionalFormatting) {
        // disable formatting for HSSF, since formulas are read incorrectly
        // and we would return incorrect results.
        return;
    }

    for (int i = 0; i < cfs.getNumConditionalFormattings(); i++) {
        ConditionalFormatting cf = cfs.getConditionalFormattingAt(i);

        List<XSSFConditionalFormattingRule> cfRuleList = getOrderedRuleList(cf);

        // rules are listen bottom up, but we want top down so that we can
        // stop when we need to. Rule indexes follow original order, because
        // that is the order CSS is applied on client side.
        for (int ruleIndex = cf.getNumberOfRules() - 1; ruleIndex >= 0; ruleIndex--) {

            ConditionalFormattingRule rule = cfRuleList.get(ruleIndex);

            // first formatting object gets 0-999, second 1000-1999...
            // should be enough.
            int cssIndex = i * 1000000 + ruleIndex * 1000;

            // build style

            // TODO: some of this code will override all old values on each
            // iteration. POI API will return the default value for nulls,
            // which is not what we want.

            StringBuilder css = new StringBuilder();

            FontFormatting fontFormatting = rule.getFontFormatting();

            if (fontFormatting != null) {
                String fontColorCSS = colorConverter.getFontColorCSS(rule);
                if (fontColorCSS != null) {
                    css.append("color:" + fontColorCSS);
                }

                // we can't have both underline and line-through in the same
                // DIV element, so use the first one that matches.

                // HSSF might return 255 for 'none'...
                if (fontFormatting.getUnderlineType() != FontFormatting.U_NONE
                        && fontFormatting.getUnderlineType() != 255) {
                    css.append("text-decoration: underline;");
                }
                if (hasStrikeThrough(fontFormatting)) {
                    css.append("text-decoration: line-through;");
                }

                if (fontFormatting.getFontHeight() != -1) {
                    // POI returns height in 1/20th points, convert
                    int fontHeight = fontFormatting.getFontHeight() / 20;
                    css.append("font-size:" + fontHeight + "pt;");
                }

                // excel has a setting for bold italic, otherwise bold
                // overrides
                // italic and vice versa
                if (fontFormatting.isItalic() && fontFormatting.isBold()) {
                    css.append("font-style: italic;");
                    css.append("font-weight: bold;");
                } else if (fontFormatting.isItalic()) {
                    css.append("font-style: italic;");
                    css.append("font-weight: initial;");
                } else if (fontFormatting.isBold()) {
                    css.append("font-style: normal;");
                    css.append("font-weight: bold;");
                }
            }

            PatternFormatting patternFormatting = rule.getPatternFormatting();
            if (patternFormatting != null) {
                String colorCSS = colorConverter.getBackgroundColorCSS(rule);

                if (colorCSS != null) {
                    css.append("background-color:" + colorCSS);
                }
            }

            cssIndex = addBorderFormatting(cf, rule, css, cssIndex);

            spreadsheet.getState().conditionalFormattingStyles.put(cssIndex, css.toString());

            // check actual cells
            runCellMatcher(cf, rule, cssIndex);

            // stop here if defined in rules
            if (stopHere(rule)) {
                break;
            }
        }

    }
}

From source file:com.vaadin.addon.spreadsheet.HSSFColorConverter.java

License:Open Source License

@Override
public String getBackgroundColorCSS(ConditionalFormattingRule rule) {
    short index = rule.getFontFormatting().getFontColorIndex();
    String styleColor = styleColor(index);
    return styleColor;
}

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

License:Apache License

private static String describeRule(ConditionalFormattingRule rule) {
    StringBuilder sb = new StringBuilder();
    sb.append("condition:");
    ConditionType ct = rule.getConditionType();
    if (ct.equals(ConditionType.CELL_VALUE_IS)) {
        sb.append(" cell value is: ");
        sb.append(describeRuleComparisonOperator(rule));
    } else if (ct.equals(ConditionType.FORMULA)) {
        sb.append(" formula: ");
        sb.append(rule.getFormula1());/*  w  w  w .  j a v  a 2 s .c om*/
    } else if (ct.equals(ConditionType.FILTER)) {
        sb.append(" filter: ");
        sb.append(describeRuleComparisonOperator(rule));
    } else if (ct.equals(ConditionType.ICON_SET)) {
        sb.append(" icon set: ");
        sb.append(rule.getMultiStateFormatting());
    } else if (ct.equals(ConditionType.COLOR_SCALE)) {
        sb.append(" color-scale: ");
        sb.append(rule.getColorScaleFormatting());
    } else if (ct.equals(ConditionType.DATA_BAR)) {
        sb.append(" data-bar: ");
        sb.append(rule.getDataBarFormatting());
    } else {
        sb.append(" type=" + rule.getConditionType());
    }
    sb.append(" formattings:");
    if (rule.getBorderFormatting() != null) {
        sb.append(" [has border formats]");
    }
    if (rule.getFontFormatting() != null) {
        sb.append(" [has font formattings]");
    }
    if (rule.getPatternFormatting() != null) {
        sb.append(" [has pattern formattings]");
    }
    return sb.toString();
}