[英]java, unable to color cells with proper color, apache poi
我想用3种不同的颜色为3套不同的列设置颜色,此方法用于添加颜色:
public static void addCellStyles(CellStyle stylex, CellStyle styley, CellStyle stylez){
stylex.setFillBackgroundColor(IndexedColors.LIGHT_GREEN.getIndex());
stylex.setFillPattern(CellStyle.SOLID_FOREGROUND);
stylex.setAlignment(CellStyle.ALIGN_CENTER);
stylex.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
styley.setFillBackgroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
styley.setFillPattern(CellStyle.SOLID_FOREGROUND);
styley.setAlignment(CellStyle.ALIGN_CENTER);
styley.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
stylez.setFillBackgroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
stylez.setFillPattern(CellStyle.SOLID_FOREGROUND);
stylez.setAlignment(CellStyle.ALIGN_CENTER);
stylez.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
}
由于某种原因,我正在将cellStyles分配给main函数中的各个单元格,单元格被涂成黑色,而且我似乎找不到任何问题可以帮助我
具有讽刺意味的是,背景颜色是POI / Excel中的前景色。
像这样尝试:
XSSFCellStyle style1 = workbook.createCellStyle();
style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128)));
style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell.setCellStyle(style1);
或者只是您的情况:
stylex.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
stylex.setFillPattern(CellStyle.SOLID_FOREGROUND);
并且不要忘记将样式分配给单元格。 如果仅从三个不同的单元格中获取初始样式,则它可能指向工作簿中的同一样式对象。 如果要使用新样式,则必须在工作簿中创建它,并将其分配给单元格(请参见我的示例)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.