使用色阶规则的XSSFSheetConditionalFormatting不会格式化使用Java的poi生成的Excel中的数据

时间:2019-02-11 12:55:32

标签: java apache-poi

我正在尝试使用XSSFWorkbook功能在Java中通过poi(4.0.1)生成的excel中创建色标条件格式。尽管我尝试了许多在线可用的示例代码变体来创建RED-YELLOW-GREEN色标格式,但我看不到数据格式的任何更改。我正在粘贴当前正在处理的代码。有人可以帮忙找出问题所在吗?

        applyColorScale(cf, "J2:J21", "00FF0000", "0000FFFF", "0000FF00");
        applyColorScale(XSSFSheetConditionalFormatting cf, String region, String... colors) {
            XSSFConditionalFormattingRule cr = cf.createConditionalFormattingColorScaleRule();
            XSSFColorScaleFormatting color = cr.createColorScaleFormatting();
            int steps = 100 / (colors.length - 1);
            for (int i = 0; i < colors.length; i++) {
                XSSFConditionalFormattingThreshold th = color.createThreshold();
                if (i == 0) {
                    th.setRangeType(XSSFConditionalFormattingThreshold.RangeType.MIN);
                } else if (i == colors.length - 1) {
                    th.setRangeType(XSSFConditionalFormattingThreshold.RangeType.MAX);
                } else {
                    th.setRangeType(XSSFConditionalFormattingThreshold.RangeType.PERCENTILE);
                }
                th.setValue((double)steps*i);
                XSSFColor clr = color.createColor();
                clr.setARGBHex(colors[i]);
            }
            CellRangeAddress[] regions = {CellRangeAddress.valueOf(region)};
            cf.addConditionalFormatting(regions, cr);
        }

Expected output in excel

1 个答案:

答案 0 :(得分:1)

如果您查看Conditional Formatting,则会找到一个链接

  

在中查看有关Excel条件格式的更多示例   ConditionalFormats.java

通过以下示例,我们得到:

FutureBuilder

代码的主要区别:创建的import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.ss.util.CellRangeAddress; import java.io.FileOutputStream; public class ConditionalFormatting { public static void applyColorScale(SheetConditionalFormatting sheetCF, String region, String... colors) { CellRangeAddress[] regions = { CellRangeAddress.valueOf(region) }; ConditionalFormattingRule rule = sheetCF.createConditionalFormattingColorScaleRule(); ColorScaleFormatting csf = rule.getColorScaleFormatting(); if (colors.length == 3) { csf.getThresholds()[0].setRangeType(ConditionalFormattingThreshold.RangeType.MIN); csf.getThresholds()[1].setRangeType(ConditionalFormattingThreshold.RangeType.PERCENTILE); csf.getThresholds()[1].setValue(50d); csf.getThresholds()[2].setRangeType(ConditionalFormattingThreshold.RangeType.MAX); for (int c = 0; c < 3; c++) { ((ExtendedColor)csf.getColors()[c]).setARGBHex(colors[c]); } } else if (colors.length == 2) { csf.setNumControlPoints(2); csf.getThresholds()[0].setRangeType(ConditionalFormattingThreshold.RangeType.MIN); csf.getThresholds()[1].setRangeType(ConditionalFormattingThreshold.RangeType.MAX); for (int c = 0; c < 2; c++) { ((ExtendedColor)csf.getColors()[c]).setARGBHex(colors[c]); } } sheetCF.addConditionalFormatting(regions, rule); } public static void main(String[] args) throws Exception { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("new sheet"); for (int r = 1; r < 20; r++) { sheet.createRow(r).createCell(9).setCellFormula("RANDBETWEEN(-100, 100)"); sheet.getRow(r).createCell(11).setCellFormula("RANDBETWEEN(-100, 100)"); } SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting(); applyColorScale(sheetCF, "J2:J21", "FFFF0000", "FFFFFF00", "FF00FF00"); applyColorScale(sheetCF, "L2:L21", "FFFF0000", "FF00FF00"); FileOutputStream out = new FileOutputStream("ConditionalFormatting.xlsx"); workbook.write(out); out.close(); workbook.close(); } } 不会在任何地方使用。只有XSSFColor才需要一个值。 RangeType.PERCENTILE中的Alpha应该是ARGB而不是FF才能完全不透明。黄色是00而不是FFFF00