Use Apache POI to Get the Background Color of the Cell in Sheets in XLSX Workbooks

Mi Guoliang

1. Add dependency in Maven

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""




2. In Java, use cell.getCellStyle().getFillForegroundColorColor() instead of cell.getCellStyle().getFillBackgroundColorColor(), is it strange? Yes, you need get the background color with the method which name looks like get the foreground color. The reason is that only the foreground tag (fgColor) is used if the fill pattern is solid. More detail about that is in the OpenXML Standard, and you could read a tiny document here

import org.apache.poi.xssf.usermodel.XSSFColor;

import java.util.Iterator;

public class Main {

    public static void main(String[] args) throws IOException {

        final String filename = args[0];

        final File file = new File(filename);

        try (final Workbook workbook = WorkbookFactory.create(file)) {

            final Sheet sheet = workbook.getSheetAt(0);

            Iterator<Row> rowIterable = sheet.rowIterator();
            while (rowIterable.hasNext()) {
                Row row =;
                Cell cell = row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                Color fillColor = cell.getCellStyle().getFillForegroundColorColor();
                System.out.println(((XSSFColor) fillColor).getARGBHex());

If you like my share, you can:

• Follow My Programming Experiences Page on Facebook.

• Follow My Twitter.

• Subscribe to my mail list.