Anne M
Anne M

Reputation: 1

unable to colour excel cell using setFillBackgroundColor + apache poi

I used below script but excel cell is not getting green color .rest evrything is working fine just green fill color is not working.

public class createExcel {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    Workbook workbook = new XSSFWorkbook();
    Sheet sheet = workbook.createSheet("Color Test");
    Row row = sheet.createRow(0);

    CellStyle style = workbook.createCellStyle();

    // style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    style.setFillBackgroundColor(IndexedColorMap.BLUE.getIndex());
    style.setFillBackgroundColor(IndexedColors.BLUE.getIndex());
    // style.setFillPattern();
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    Font font = workbook.createFont();
    font.setColor(IndexedColors.RED.getIndex());
    style.setFont(font);

    Cell cell1 = row.createCell(0);
    cell1.setCellValue("ID");
    cell1.setCellStyle(style);

    Cell cell2 = row.createCell(1);
    cell2.setCellValue("NAME");
    cell2.setCellStyle(style);

    FileOutputStream fos = new FileOutputStream(new File("D:\\backup\\Mine\\applications\\PuneetPRo\\logged.xlsx"));
    workbook.write(fos);
    fos.close();
    System.out.println("Done");
}

}

Upvotes: 0

Views: 143

Answers (1)

JLazar0
JLazar0

Reputation: 1292

To set cell color, use

style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

instead of

style.setFillBackgroundColor(IndexedColors.BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

Upvotes: 1

Related Questions