skip to Main Content

I use this code to change the font in excel to color, which I define

        Color sColor = new Color (value,0,0);
        XSSFColor userColor = new XSSFColor(sColor);

        CellStyle style = wb.createCellStyle();
        XSSFFont font = wb.createFont();

        font.setColor(userColor);
        style.setFont(font);
        cell.setCellStyle(style);

Can I change the cell’s Background same way.

I saw the question here
Setting background custom color not working for XSSF in Apache POI
And I used the code:

        XSSFCellStyle cellStyle = wb.createCellStyle();
        XSSFColor color = new XSSFColor(new java.awt.Color(value, 0, 0));
        ((XSSFCellStyle)cellStyle).setFillBackgroundColor(color);

        cell.setCellStyle(cellStyle);

The background still always white.

Am sure that all other parts of the code written properly, because it works when I changed the font.

I have office 2010 on my computer

2

Answers


  1. Chosen as BEST ANSWER

    Thanks all I found solution now

            XSSFCellStyle cellStyle = wb.createCellStyle();
    
            XSSFColor color = new XSSFColor(new java.awt.Color(value, 0, 0));
    
            ((XSSFCellStyle)cellStyle).setFillBackgroundColor(color);
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            cellStyle.setFillForegroundColor(color); 
            cell.setCellStyle(cellStyle);
    

  2. Create cell style object:

    CellStyle backgroundStyle = workbook.createCellStyle(); 
    

    Set custom color:

    backgroundStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
    

    Add style to the cell:

    cell.setCellStyle(backgroundStyle);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search