求问poi怎样读取excel条件格式的字体颜色

求问poi怎样读取excel条件格式的字体颜色,求问poi怎样读取excel条件格式的字体颜色,求问poi怎样读取excel条件格式的字体颜色,求问poi怎样读取excel条件格式的字体颜色

 HSSFRow row = sheet.getRow(某行);
HSSFCell cell = row.getCell((short)某列);
HSSFCellStyle cellStyle = cell.getCellStyle();
HSSFFont font = cellStyle.getFont(workbook);
font = getColor();

在国外网站查到的,大概思路就是,读取cell的所有条件格式,然后用POI的解释器,执行一下条件,然后就能取到改变的颜色了。


   static List<EvaluationConditionalFormatRule> getMatchingConditionalFormattingForCell(Cell cell) {
        Sheet sheet = cell.getSheet();
        Workbook workbook = sheet.getWorkbook();
        WorkbookEvaluatorProvider workbookEvaluatorProvider =
                (WorkbookEvaluatorProvider)workbook.getCreationHelper().createFormulaEvaluator();
        ConditionalFormattingEvaluator conditionalFormattingEvaluator =
                new ConditionalFormattingEvaluator(workbook, workbookEvaluatorProvider);
        List<EvaluationConditionalFormatRule> matchingCFRulesForCell =
                conditionalFormattingEvaluator.getConditionalFormattingForCell(cell);
        return matchingCFRulesForCell;
    }


public static void test(String path) throws Exception{
        Workbook workbook = WorkbookFactory.create(new FileInputStream(path));
        Sheet sheet = workbook.getSheetAt(0);

        for (Row row : sheet) {
            for (Cell cell : row) {
                System.out.println(cell.getAddress());
                List<EvaluationConditionalFormatRule> matchingCFRules = getMatchingConditionalFormattingForCell(cell);
                System.out.println(matchingCFRules);
                for (EvaluationConditionalFormatRule evalCFRule : matchingCFRules) {
                    ConditionalFormattingRule cFRule = evalCFRule.getRule();
                    if (cFRule.getPatternFormatting() != null) {
                        PatternFormatting patternFormatting = cFRule.getPatternFormatting();
                        System.out.println(((XSSFColor)patternFormatting.getFillBackgroundColorColor()).getARGBHex());
                    } else if (cFRule.getColorScaleFormatting() != null) {
                        System.out.println("has color scale formatting: " + cFRule.getColorScaleFormatting());
                    }
                }

                System.out.println();
            }
        }

        workbook.close();

    }