使用java操作excel时可以指定单元格的颜色,有两种方法:
1.使用提供的索引:
//设置样式-颜色HSSFCellStyle style = workbook.createCellStyle();style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); HSSFColor.SKY_BLUE.index 这里的颜色是java提供的
2.自定义颜色
比如这里有一个16进制的字符串用来表示颜色,,通过下面的方法来自定义颜色:
String color = "cbfdee"; //此处得到的color为16进制的字符串//转为RGB码int r = Integer.parseInt((color.substring(0,2)),16); //转为16进制int g = Integer.parseInt((color.substring(2,4)),16);int b = Integer.parseInt((color.substring(4,6)),16);//自定义cell颜色HSSFPalette palette = workbook.getCustomPalette();//这里的9是索引palette.setColorAtIndex((short)9, (byte) r, (byte) g, (byte) b);HSSFCellStyle style = workbook.createCellStyle();style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);style.setFillForegroundColor((short)9);然后cell.setCellStyle(style);即可将样式赋给指定单元格
版权声明:本文为博主原创文章,未经博主允许不得转载。
告诉自己,我这次失败了,