您的位置:首页 > 其它

Poi 操作excel 定义单元格颜色

2015-10-22 14:51 459 查看
使用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);即可将样式赋给指定单元格
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: