导出多种颜色主要有两个细节要注意 否则就只会输出最后一种颜色
第一点: HSSFCellStyle style = wb.createCellStyle(); 这个对象要创建在循环体里面 每一次都获得新的style 。 第二点: 如果你要的颜色 系统自带的够用就不用管第二点,如果不够 就需要注意了 。 HSSFPalette 类提供了 findSimilarColor 方法可以查找相似颜色 需要传入RGB值 HSSFColor COLOR = customPalette.findSimilarColor((byte) RGB1, (byte) RGB2, (byte) RGB3); 但是 这个方法又非常不靠谱 它只是根据RGB值查找相似颜色的下标 会有很大的偏差 而且 不同的颜色也可能会获取到相同的下标 所以如果不处理 那么输出的颜色 也会有冲突 所以你要有一个处理颜色的方法 把 起冲突的 用系统自带的输出 不起冲突的用 RGB值输出 就可以了 话不多说 上代码
这是设置颜色部分
String[] RGB = exportDto.getAsString("COLOR_RGB_"+i+"_"+j).split(",");
if(SysUtils.isNotEmpty(RGB) && RGB.length == 3){
int RGB1 = Integer.valueOf(RGB[0]);
int RGB2 = Integer.valueOf(RGB[1]);
int RGB3 = Integer.valueOf(RGB[2]);
HSSFCellStyle style = wb.createCellStyle();
//获取颜色
short index = GetColor(RGB1, RGB2, RGB3);
if(index > 0){
style.setFillForegroundColor(index);
}else{
//不会冲突的 通过RGB 查到颜色对象
HSSFColor COLOR = customPalette.findSimilarColor((byte) RGB1, (byte) RGB2, (byte) RGB3);
//设置颜色
customPalette.setColorAtIndex(COLOR.getIndex(), (byte) RGB1, (byte) RGB2, (byte) RGB3);
//设置单元格背景颜色
style.setFillForegroundColor(COLOR.getIndex());
}
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//指定是哪一列那一行
HSSFRow row = sheet1.getRow(color_1);
HSSFCell cell = row.createCell(color_2);
cell.setCellStyle(style);
}
下面是通过 我自己有的RGB颜色 对比出来起冲突的颜色
public short GetColor(int RGB_1, int RGB_2, int RGB_3) throws Exception {
short index = 0;
if(RGB_1 == 255 && RGB_2 == 255 && RGB_3 == 255){
// 单色
index = HSSFColor.WHITE.index;
}else if(RGB_1 == 255 && RGB_2 == 96 && RGB_3 == 175){
// 梅红
index = HSSFColor.PINK.index;
}else if(RGB_1 == 247 && RGB_2 == 80 && RGB_3 == 0){
// 桔红
index = HSSFColor.ORANGE.index;
}else if(RGB_1 == 255 && RGB_2 == 81 && RGB_3 == 81){
// 西瓜红
index = HSSFColor.CORAL.index;
}else if(RGB_1 == 159 && RGB_2 == 80 && RGB_3 == 0){
// 棕色
index = HSSFColor.BROWN.index;
}else if(RGB_1 == 240 && RGB_2 == 240 && RGB_3 == 240){
// 银色
index = HSSFColor.GREY_25_PERCENT.index;
}else if(RGB_1 == 122 && RGB_2 == 254 && RGB_3 == 198){
// 浅绿
index = HSSFColor.LIGHT_GREEN.index;
}else if(RGB_1 == 1 && RGB_2 == 129 && RGB_3 == 74){
// 军绿
index = HSSFColor.DARK_GREEN.index;
}else if(RGB_1 == 255 && RGB_2 == 177 && RGB_3 == 177){
// 橡皮红
index = HSSFColor.CORAL.index;
}else if(RGB_1 == 121 && RGB_2 == 255 && RGB_3 == 121){
// 果绿
index = HSSFColor.LIGHT_GREEN.index;
}else if(RGB_1 == 255 && RGB_2 == 128 && RGB_3 == 0){
// 橙色
index = HSSFColor.LIGHT_ORANGE.index;
}else if(RGB_1 == 185 && RGB_2 == 185 && RGB_3 == 115){
// 卡其色
index = HSSFColor.DARK_YELLOW.index;
}else if(RGB_1 == 187 && RGB_2 == 61 && RGB_3 == 0){
// 锈红
index = HSSFColor.BROWN.index;
}else if(RGB_1 == 255 && RGB_2 == 121 && RGB_3 == 188){
// 深粉
index = HSSFColor.ROSE.index;
}else if(RGB_1 == 40 && RGB_2 == 255 && RGB_3 == 40){
// 荧光绿
index = HSSFColor.BRIGHT_GREEN.index;
}else if(RGB_1 == 244 && RGB_2 == 244 && RGB_3 == 244){
// 烟灰色
index = HSSFColor.GREY_40_PERCENT.index;
}else if(RGB_1 == 255 && RGB_2 == 244 && RGB_3 == 193){
// 米黄
index = HSSFColor.LEMON_CHIFFON.index;
}else if(RGB_1 == 255 && RGB_2 == 217 && RGB_3 == 236){
// 粉色
index = HSSFColor.ROSE.index;
}else if(RGB_1 == 255 && RGB_2 == 252 && RGB_3 == 236){
// 米白
index = HSSFColor.WHITE.index;
}
return index;
}
这样就可以输出多种颜色了 第一次发表 之前找了好久才找到一篇借鉴
萌新一枚 勿喷
借鉴的这一篇
https://blog.csdn.net/drifterj/article/details/46662277