Excelで「値が一定以上ならセルの色を変える」といった条件付き書式は、視認性を高めるためによく使われます。
この記事では、JavaのApache POIライブラリを使って、Excelに条件付き書式を自動的に設定する方法を解説します。
この記事でわかること
- Apache POIで条件付き書式を設定する基本構文
- 数値条件で色を変えるサンプル
- 文字列一致や数式条件の応用例
前提:Apache POIの準備
Mavenを使っている場合、以下の依存関係をpom.xml
に追加してください。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
基本:数値に応じてセルの背景色を変える
以下のコードは、「セルの値が80以上なら背景色を緑、それ未満なら赤」に設定するサンプルです。
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
public class ConditionalFormattingExample {
public static void main(String[] args) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("スコア");
// データの入力
for (int i = 0; i < 10; i++) {
Row row = sheet.createRow(i);
Cell cell = row.createCell(0);
cell.setCellValue(60 + i * 5); // 60, 65, ..., 105
}
// 条件付き書式の設定
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
// 条件①:値が80以上 → 緑背景
ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(
ComparisonOperator.GE, "80");
PatternFormatting fill1 = rule1.createPatternFormatting();
fill1.setFillBackgroundColor(IndexedColors.LIGHT_GREEN.getIndex());
fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
// 条件②:値が80未満 → 赤背景
ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(
ComparisonOperator.LT, "80");
PatternFormatting fill2 = rule2.createPatternFormatting();
fill2.setFillBackgroundColor(IndexedColors.ROSE.getIndex());
fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
// 書式適用範囲
CellRangeAddress[] regions = {
CellRangeAddress.valueOf("A1:A10")
};
// 書式をシートに登録
sheetCF.addConditionalFormatting(regions, rule1, rule2);
// 出力
try (FileOutputStream fos = new FileOutputStream("conditional_formatting.xlsx")) {
workbook.write(fos);
}
workbook.close();
System.out.println("条件付き書式を設定しました。");
}
}
応用①:文字列の一致条件で色を変える
「セルの値が ‘合格’ なら青、それ以外はグレー」にする例です。
ConditionalFormattingRule ruleOK = sheetCF.createConditionalFormattingRule(
"A1=\"合格\"");
PatternFormatting fmtOK = ruleOK.createPatternFormatting();
fmtOK.setFillBackgroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
fmtOK.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
ConditionalFormattingRule ruleNG = sheetCF.createConditionalFormattingRule(
"A1<>\"合格\"");
PatternFormatting fmtNG = ruleNG.createPatternFormatting();
fmtNG.setFillBackgroundColor(IndexedColors.GREY25_PERCENT.getIndex());
fmtNG.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
sheetCF.addConditionalFormatting(
CellRangeAddress.valueOf("A1:A10"),
ruleOK, ruleNG
);
"A1=\"合格\""
のように、数式で条件を記述します。
応用②:複雑な数式を条件に使う
「列Bの値がCより大きい場合に背景を黄色にする」など、複雑な条件も可能です。
ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule("B1>C1");
PatternFormatting fill = rule.createPatternFormatting();
fill.setFillBackgroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
fill.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
sheetCF.addConditionalFormatting(
CellRangeAddress.valueOf("B1:B10"),
rule
);
このようにセル参照を活用すれば、柔軟なルールを作成できます。
注意点
- 条件式に使うセル参照は左上セル基準で記述(例:
A1
) setFillBackgroundColor()
よりもsetFillForegroundColor()
のほうが視認性が高い- XSSF(.xlsx)形式での使用を想定しています(HSSFは一部制限あり)
まとめ
Apache POIを使えば、Excelファイルに対してプログラムで柔軟に条件付き書式を適用できます。
大量の帳票や報告書を扱う場面で、視認性の高い資料を自動生成したい方はぜひ活用してみてください。