我正在使用Apache poi和XLSX文件。我使用xssf类来动态创建电子表格。 我想设置单元格样式在for循环中,但它似乎并没有工作......这里是我的代码:设置单元格样式不起作用
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);
...这我只是一个代码的一部分... 我不明白为什么不工作。似乎cellstyle被忽略或overwrited ....
任何线索?
我已经尝试了你的方法,但没有解决任何问题...我也致力于创建一个.xlsx模型文件,但是我正在生成一个基于用户在运行时定义的一些标准的100%动态电子表格... – Medioman92
此外,如果我使用“IndexedColor”类来设置背景,它总是会产生黑色 – Medioman92
在答案的底部查看我的建议 - 在excel中根据需要创建它,从POI读取它并制定出来什么选项需要设置,使样式看起来像你想 – Gagravarr