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();
}
}
}
More than 1 year has passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme