使用 Apache POI 自定义交通灯的条件格式

Customize Traffic Lights' Conditional Formatting with Apache POI

我阅读了相关问题并自定义了红绿灯的值。

The IconMultiStateFormatting has following thresholds per default:

  • If the cell value is greater than or equal 66% of all the values in the range, then green.
  • If the cell value is lower but greater than or equal 33% of all the values in the range, then yellow.
  • If the cell value is lower than 33% of all the values in the range, then red.

我需要的是交换红灯和绿灯(红色为大量,绿色为较低值)可以吗?

目前,这是我的代码:

SheetConditionalFormatting sheetCF = currentSheet.getSheetConditionalFormatting()
ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule(IconMultiStateFormatting.IconSet.GYR_3_TRAFFIC_LIGHTS)
rule.getMultiStateFormatting().setIconOnly(false)
ConditionalFormattingThreshold[] thresholds = rule.getMultiStateFormatting().getThresholds()
if (thresholds.length == 3) {
    (0..2).each { i ->
        ConditionalFormattingThreshold threshold = thresholds[i]
        println("-------------------- $i : ${threshold.getRangeType()}")   // 4 - percent
        println("-------------------- $i : ${threshold.getValue()}")
        println("-------------------- $i : ${threshold.getFormula()}")     // null
        threshold.setRangeType(ConditionalFormattingThreshold.RangeType.PERCENT)
        switch (i) {
            case 0:  // RED LIGHT
                threshold.setValue(50.0)
                break
            case 1:  // YELLOW LIGHT
                threshold.setValue(20.0)
                break
            case 2:  // GREEN LIGHT
                threshold.setValue(0.0)
                break
        }
    }
}
ConditionalFormattingRule [] cfRules = [ rule ]
CellRangeAddress [] regions = [ CellRangeAddress.valueOf("F2:F$lastRow") ]
sheetCF.addConditionalFormatting(regions, cfRules)

在上面的这段代码中,我假装得到绿色 <= 20%;黄色 20%-50%;红色 > 50%.

如何在 Excel 中进行设置?我看到的唯一方法是使用相反的图标顺序。 IconMultiStateFormatting 也提供了这个。

完整示例:

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import org.apache.poi.ss.util.CellRangeAddress;

import java.io.FileOutputStream;

class ConditionalFormattingIconSet {

 public static void main(String[] args) throws Exception {

  Workbook workbook = new XSSFWorkbook();

  Sheet sheet = workbook.createSheet("Sheet1");

  CellStyle cellStyle = workbook.createCellStyle();
  cellStyle.setAlignment(HorizontalAlignment.CENTER); 
  cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); 

  Cell cell = null;
  for (int r = 0; r < 10; r++) {
   cell = sheet.createRow(r).createCell(0);
   cell.setCellValue(r+1);
   cell.setCellStyle(cellStyle);
  }

  SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

  ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule(IconMultiStateFormatting.IconSet.GYR_3_TRAFFIC_LIGHTS);

  //rule.getMultiStateFormatting().setIconOnly(true);

  IconMultiStateFormatting iconMultiStateFormatting = rule.getMultiStateFormatting();
  ConditionalFormattingThreshold[] thresholds = iconMultiStateFormatting.getThresholds();
  if (thresholds.length == 3) {
   for (int i = 0; i < 3; i++) {
    ConditionalFormattingThreshold threshold = thresholds[i];
System.out.println(i + " : " + threshold.getRangeType()); // default 
System.out.println(i + " : " + threshold.getValue()); // default
    // default = green if >= 66%; yellow if < 66% and >= 33%, red if < 33%

    // changing the thresholds to green if >= 50%; yellow if < 50% and >= 20%, red if < 20%
    if (i == 0) {
     threshold.setValue(0d);
    } else if (i == 1) {
     threshold.setValue(20d);
    } else if (i == 2) {
     threshold.setValue(50d);
    }
   }  
   // set reversed icon order since you wants red if >= 50%; yellow if < 50% and >= 20%, green if < 20%
   iconMultiStateFormatting.setReversed(true);
  }

  ConditionalFormattingRule [] cfRules = {rule};

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

  sheetCF.addConditionalFormatting(regions, cfRules);

  FileOutputStream fileOut = new FileOutputStream("ConditionalFormattingIconSet.xlsx");
  workbook.write(fileOut);
  fileOut.close();

 }
}