You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Apache POI与Java创建条件格式渐变填充数据条?

用Apache POI实现Excel渐变填充数据条

我创建的Excel文件里存有一组百分比数据,在Excel客户端中可选中这些数据,通过「开始→条件格式→数据条→渐变填充」设置样式。现需使用Apache POI和Java实现该功能,请求补全以下示例代码中的空白部分:

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;

public class ConditionalFormattingExample {
public static void main(String[] args) throws Exception {
    Workbook workbook = new XSSFWorkbook();
    Sheet sheet = workbook.createSheet("new sheet");
    String[] list = new String[]{"5%", "-5%", "10%", "-25%"};
    for (int i = 0; i < list.length; i++) {
        sheet.createRow(i).createCell(0).setCellValue(list[i]);
    }
    SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
    
    //--???
    
    FileOutputStream out = new FileOutputStream("ConditionalFormattingExample.xlsx");
    workbook.write(out);
    out.close();
    workbook.close();
}
}

补全后的完整代码

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

import java.io.FileOutputStream;

public class ConditionalFormattingExample {
    public static void main(String[] args) throws Exception {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("new sheet");
        String[] list = new String[]{"5%", "-5%", "10%", "-25%"};
        for (int i = 0; i < list.length; i++) {
            sheet.createRow(i).createCell(0).setCellValue(list[i]);
        }
        SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

        // 创建无比较条件的规则(数据条属于此类格式)
        ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule(ComparisonOperator.NO_COMPARISON);
        // 获取数据条格式对象
        DataBarFormatting dataBar = rule.getDataBarFormatting();
        // 开启渐变填充(仅XSSF支持此设置)
        if (dataBar instanceof XSSFDataBarFormatting) {
            ((XSSFDataBarFormatting) dataBar).setGradient(true);
        }
        // 设置数据条的最小/最大值为自动计算(基于单元格数据范围)
        dataBar.getMinThreshold().setRangeType(ConditionalFormattingThreshold.RangeType.AUTO);
        dataBar.getMaxThreshold().setRangeType(ConditionalFormattingThreshold.RangeType.AUTO);
        // 设置数据条颜色(可自定义)
        dataBar.setColor(workbook.getCreationHelper().createIndexedColors().getIndexedColor(IndexedColors.BLUE.getIndex()));
        // 指定应用条件格式的单元格区域
        CellRangeAddress[] regions = {CellRangeAddress.valueOf("A1:A4")};
        // 将规则添加到工作表
        sheetCF.addConditionalFormatting(regions, rule);

        FileOutputStream out = new FileOutputStream("ConditionalFormattingExample.xlsx");
        workbook.write(out);
        out.close();
        workbook.close();
    }
}

关键说明

  • 数据条属于无需比较运算符的条件格式,因此创建NO_COMPARISON类型的规则
  • 渐变填充是XSSF(.xlsx格式)专属特性,需要强转类型后开启
  • 设置阈值为自动,让Excel根据单元格内的百分比数据自动计算数据条长度
  • 可通过setColor()方法自定义数据条的显示颜色

内容的提问来源于stack exchange,提问作者user769552

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 16:48:00