Tatets
Tatets

Reputation: 3

Apache poi 3.10. HSSFFONT setColor doesnt work

I try to have cells with white colored font but they always appear black, this is my code:

HSSFCellStyle estiloCabecera = wb.createCellStyle();
estiloCabecera.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
estiloCabecera.setFillPattern(CellStyle.SOLID_FOREGROUND);

HSSFFont fuenteCabecera = wb.createFont();
fuenteCabecera.setFontHeightInPoints((short)12);
fuenteCabecera.setFontName("Arial");
fuenteCabecera.setColor((short)HSSFColor.WHITE.index);
//fuenteCabecera.setColor(HSSFFont.COLOR_RED);
estiloCabecera.setFont(fuenteCabecera);

Row rowCabeceraTabla = sheet.createRow((short)3);
Cell celda = rowCabeceraTabla.createCell(0);
celda.setCellValue(new HSSFRichTextString("Id pregunta"));
celda.setCellStyle(estiloCabecera);

I dont understand why, and I have tried everything I can think of. Can anyone help, please?

Upvotes: 0

Views: 4980

Answers (1)

kiwiwings
kiwiwings

Reputation: 3446

As posted in my comment, your example works for me, i.e. I get a black cell with white text.

The rectangle(s) instead of the text normally points out, that you've got an encoding problem or the font is not supporting the chars. Instead of "Arial" can you try to use "Arial Unicode MS"?

I'm posting my code as an answer just for further reference ... and will update it, if we found out new details, which could solve your issue ...

import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;


public class ForegroundColor {
    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet();

        CellStyle estiloCabecera = wb.createCellStyle();
        estiloCabecera.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
        estiloCabecera.setFillPattern(CellStyle.SOLID_FOREGROUND);

        Font fuenteCabecera = wb.createFont();
        fuenteCabecera.setFontHeightInPoints((short)12);
        fuenteCabecera.setFontName("Arial");
        fuenteCabecera.setColor((short)HSSFColor.WHITE.index);
        //fuenteCabecera.setColor(HSSFFont.COLOR_RED);
        estiloCabecera.setFont(fuenteCabecera);

        Row rowCabeceraTabla = sheet.createRow((short)3);
        Cell celda = rowCabeceraTabla.createCell(0);
        celda.setCellValue(new HSSFRichTextString("Id pregunta"));
        celda.setCellStyle(estiloCabecera);

        FileOutputStream fos = new FileOutputStream("color.xls"); 
        wb.write(fos);
        fos.close();
    }
}

Upvotes: 1

Related Questions