CellStyles is a workbook, and there is a hard limit that Excel imposes on the numbers that a file is allowed to have, so you need to make sure that you are creating a cell style outside of the loop.
Your code will look something like this:
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); 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){ 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); } }
If you are having problems with a style that doesn't look the way you expect, the best option is to style the cell as you want in Excel, save the file, read it in the POI and view the cell style that Excel wrote. Excel can sometimes do some strange things that get used to, so check what it does to understand what you need to do!
Gagravarr
source share