Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting cell style not working

i'm working with apache poi and XLSX file. i use xssf classes to dynamically create a spreadsheet. i'd like to set cell's style in a for loop, but it doesn't seem to work...here's my code :

for(int i=1;i<=gc.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);i++,gc.add(GregorianCalendar.DATE, 1),righe++){
        Row r = foglio.createRow(righe);

        if(getDayOfWeek(gc)== 6 || getDayOfWeek(gc) == 7){
            XSSFCellStyle cs1 = wb.createCellStyle();
            cs1.setFillBackgroundColor(IndexedColors.YELLOW.getIndex());
            cs1.setFillPattern(CellStyle.SOLID_FOREGROUND);
            XSSFFont f = wb.createFont();
            f.setBold(true);
            f.setColor(IndexedColors.RED.getIndex());
            cs1.setFont(f);
            Cell c1 = r.createCell(0);
                 c1.setCellValue(cost.getGiorni().get(getDayOfWeek(gc)-1).getNomeGiorno());
                 c1.setCellStyle(cs1);
            Cell c2 = r.createCell(1);
                 c2.setCellValue(i);
                 c2.setCellStyle(cs1);
        }               
        r.createCell(0).setCellValue(cost.getGiorni().get(getDayOfWeek(gc)-1).getNomeGiorno());
        r.createCell(1).setCellValue(i);

...this i just a portion of the code... i can't understand why is not working. Seems like the cellstyle is ignored or overwrited....

any clue ?

like image 758
Medioman92 Avatar asked Jul 09 '13 10:07

Medioman92


1 Answers

You can use Following method, perhaps this will resolve your problem.

public static void setCellColorAndFontColor(XSSFCell cell, IndexedColors FGcolor, IndexedColors FontColor ){
    XSSFWorkbook wb = cell.getRow().getSheet().getWorkbook();
    CellStyle style = wb.createCellStyle();
    XSSFFont font = wb.createFont();
    font.setBold(true);
    font.setColor(FontColor.getIndex());
    style.setFont(font);
    style.setFillForegroundColor(FGcolor.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    cell.setCellStyle(style);
}

When you are calling this method the way should be like

setCellColorAndFontColor(cell, IndexedColors.BLACK, IndexedColors.WHITE);

will create bold & white font text color with black cell background color in the sheet.

like image 154
Sankumarsingh Avatar answered Oct 20 '22 16:10

Sankumarsingh