Example usage for org.apache.poi.hssf.usermodel HSSFConditionalFormattingRule getPatternFormatting

List of usage examples for org.apache.poi.hssf.usermodel HSSFConditionalFormattingRule getPatternFormatting

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFConditionalFormattingRule getPatternFormatting.

Prototype

public HSSFPatternFormatting getPatternFormatting() 

Source Link

Usage

From source file:cn.trymore.core.util.excel.PoiExcelParser.java

License:Open Source License

private void handleContionalFormatCell(HSSFConditionalFormattingRule rule, HSSFCell cell) {
    if ((cell != null) && (rule != null)) {

        Object cellValue = getCellContent(cell);
        String formula1 = rule.getFormula1();
        String formula2 = rule.getFormula2();
        String fmtColor = tripleToRGBString(rule.getPatternFormatting().getFillBackgroundColor());

        if (this.cfmCells.containsKey(cell)) {
            return;
        }/*from   w w w. j  a v a  2 s. c  o  m*/

        if (cellValue == null && cellValue instanceof Double) {
            cellValue = 0.0;
        }

        try {
            //            switch (cell.getCellType()) {
            //               case 0:
            //               case 2:
            //               case 3:
            switch (rule.getConditionType()) {
            case ExcelConstants.ConditionType.CELL_VALUE_IS:
                switch (rule.getComparisonOperation()) {
                case ExcelConstants.ComparisionType.BETWEEN: //1
                    if (cellValue instanceof Double) {
                        if ((Double.parseDouble(cellValue.toString()) >= Double.parseDouble(formula1))
                                && (Double.parseDouble(cellValue.toString()) <= Double.parseDouble(formula2))) {
                            this.cfmCells.put(cell, fmtColor);
                            return;
                        }
                    }
                    break;
                case ExcelConstants.ComparisionType.EQUALTO: //3
                    if (cellValue instanceof String) {
                        if (("\"" + cellValue.toString() + "\"").equals(formula1)) {
                            this.cfmCells.put(cell, fmtColor);
                            return;
                        }
                    } else if (cellValue instanceof Double) {
                        if (Double.parseDouble(cellValue.toString()) == Double.parseDouble(formula1)) {
                            this.cfmCells.put(cell, fmtColor);
                            return;
                        }
                    }
                    break;
                case ExcelConstants.ComparisionType.GREATERTHAN: //5
                    if (Double.parseDouble(cellValue.toString()) > Double.parseDouble(formula1)) {
                        this.cfmCells.put(cell, fmtColor);
                        return;
                    }
                    break;
                case ExcelConstants.ComparisionType.GREATERTHAN_OR_EQUALTO: //7
                    if (Double.parseDouble(cellValue.toString()) >= Double.parseDouble(formula1)) {
                        this.cfmCells.put(cell, fmtColor);
                        return;
                    }
                    break;
                case ExcelConstants.ComparisionType.LESSTHAN: //6
                    if (Double.parseDouble(cellValue.toString()) < Double.parseDouble(formula1)) {
                        this.cfmCells.put(cell, fmtColor);
                        return;
                    }
                    break;
                case ExcelConstants.ComparisionType.LESSTHAN_OR_EQUALTO: //8
                    if (Double.parseDouble(cellValue.toString()) <= Double.parseDouble(formula1)) {
                        this.cfmCells.put(cell, fmtColor);
                        return;
                    }
                    break;
                case ExcelConstants.ComparisionType.NOT_EQUALTO: //4
                    if (Double.parseDouble(cellValue.toString()) != Double.parseDouble(formula1)) {
                        this.cfmCells.put(cell, fmtColor);
                        return;
                    }
                    break;
                case ExcelConstants.ComparisionType.NOTBETWEEN: //2
                    if ((Double.parseDouble(cellValue.toString()) < Double.parseDouble(formula1))
                            && (Double.parseDouble(cellValue.toString()) > Double.parseDouble(formula2))) {
                        this.cfmCells.put(cell, fmtColor);
                        return;
                    }
                    break;
                }
            case ExcelConstants.ConditionType.FORMULA_IS:
                break;
            }
            //            }
        } catch (Exception e) {
            this.cfmCells.put(cell, "#FFFFFF");
            logger.error("handling the contional format cell errors: " + e);
        }
    }
}