java poi 解决 excel 条件格式筛选单元格问题

最近因项目需要我需要知道怎么用poi解析出是xlsx 的excel 用条件格式中规则而导致单元格有颜色的单元格

已经尝试过:
1. 单元格颜色筛选--- two.getCellStyle().getFillForegroundColor() 失败,全部为64 自动
2. 单元格填充模式筛选--- two.getCellStyle().getFillPattern() 失败,全部为0
3.单元格边框颜色筛选--- two.getCellStyle().getBottomBorderColor() 失败,全部为8
寻求帮助,谢谢。

你把这个xlsx文件的内容 复制一下,复制到一个新建的空的xlsx文件中,再试试

如果是Microsoft Excel 97-2003 工作表 (.xls)

if(31 == cell.getCellStyle().getFillForegroundColor()) //判断单元格前景色为淡蓝色
if(10 == book.getFontAt(cell.getCellStyle().getFontIndex()).getColor()) //判断单元格字体颜色为红色

如果是Microsoft Excel 工作表 (.xlsx)

if(0 == cell.getCellStyle().getFillForegroundColor()) //判断单元格前景色为淡蓝色
if(0 == book.getFontAt(cell.getCellStyle().getFontIndex()).getColor()) //判断单元格字体颜色为红色

这说明通过poi获得单元格颜色或字体格式,得到的都是数字码,需要转换为颜色或字体对应才能使用,你可以搜一下对应关系

楼主问题解决了吗?我也遇到这个问题,如果解决麻烦给个解决办法,谢谢啊

获取表格内的所偶 条件,然后用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();
    }