加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Poi 操作excel 定义单元格颜色

发布时间:2020-12-13 21:05:46 所属栏目:PHP教程 来源:网络整理
导读:使用java操作excel时可以指订单元格的色彩,有两种方法: 1.使用提供的索引: //设置样式-色彩 HSSFCellStyle style = workbook.createCellStyle(); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setFillForegroundColor(HSSFColor.SKY_BLU

使用java操作excel时可以指订单元格的色彩,有两种方法:

1.使用提供的索引:


//设置样式-色彩 HSSFCellStyle style = workbook.createCellStyle(); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
HSSFColor.SKY_BLUE.index  这里的色彩是java提供的



2.自定义色彩

比如这里有1个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);便可将样式赋给指订单元格


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读