gpt4 book ai didi

java - 使用 POI 库进行条件格式化

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:40 25 4
gpt4 key购买 nike

我在 POI 条件格式方面遇到了一些问题。我不完全了解 POI 在这里做什么。 我正在为值超过 70 的单元格值设置背景颜色格式规则。我想在我的应用程序中获取 CellStyle(通过条件格式规则应用),但 POI 不会返回更新的单元格样式,而是返回默认样式.这是我的代码

            XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();

sheetConditionalFormatting sheetCF = sheet
.getSheetConditionalFormatting();



// Condition 1: Cell Value Is greater than 70 (Blue Fill)
ConditionalFormattingRule rule1 = sheetCF
.createConditionalFormattingRule(ComparisonOperator.GT, "70");
PatternFormatting fill1 = rule1.createPatternFormatting();
fill1.setFillBackgroundColor(IndexedColors.BLUE.index);
fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);

CellRangeAddress[] regions = { CellRangeAddress.valueOf("A1:C10") };

int index = sheetCF.addConditionalFormatting(regions, rule1);

sheet.createRow(0).createCell(0).setCellValue(84);
sheet.createRow(1).createCell(0).setCellValue(60);
sheet.createRow(2).createCell(0).setCellValue(50);
sheet.createRow(3).createCell(0).setCellValue(51);
sheet.createRow(4).createCell(0).setCellValue(49);
sheet.createRow(5).createCell(0).setCellValue(41);

Cell cell = sheet.getRow(0).getCell(0);
CellStyle style = cell.getCellStyle();
System.out.println("style index : "+style.getIndex()+" value:"+cell.getNumericCellValue());

使用上面的代码,style.getIndex() 总是返回 0(即默认格式)。我觉得它应该返回带有背景颜色的更新格式样式。当我在实际的 xlsx 文件中编写上述工作簿并使用 MSExcel 打开时,我可以看到第一个单元格的背景颜色。同样,当我将它从 xlsx 文件读取到 POI 工作簿时,它不会返回具有背景颜色的单元格样式。

有没有人尝试过/遇到过同样的问题?

问候,爱资哈尔

最佳答案

getCellStyle 返回与单元格关联的格式样式。应用条件后,它不会返回评估的格式样式样式。为此,您需要 ConditionalFormattingEvaluator

关于java - 使用 POI 库进行条件格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19178354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com