0
0

Apache POIを使用してExcelのセルの色を取得する

Posted at
import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.IOException;

public class ExcelCellColorReader {
    public static void main(String[] args) {
        try {
            FileInputStream file = new FileInputStream("path/to/your/excel/file.xlsx");
            Workbook workbook = WorkbookFactory.create(file);

            // シートのインデックスを指定
            Sheet sheet = workbook.getSheetAt(0);

            // セルの座標を指定 (例: B3セル)
            int rowNumber = 2; // 0から始まる行番号
            int columnNumber = 1; // 0から始まる列番号

            Row row = sheet.getRow(rowNumber);
            if (row != null) {
                Cell cell = row.getCell(columnNumber);
                if (cell != null) {
                    CellStyle cellStyle = cell.getCellStyle();
                    Color color = cellStyle.getFillForegroundColorColor();

                    if (color != null) {
                        System.out.println("セルの色: " + color.toString());
                    } else {
                        System.out.println("セルの色は設定されていません。");
                    }
                } else {
                    System.out.println("セルが存在しません。");
                }
            } else {
                System.out.println("行が存在しません。");
            }

            workbook.close();
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0