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);即可将样式赋给指定单元格
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);即可将样式赋给指定单元格
相关文章推荐
- 使用ss命令代替netstat 统计TCP网络连接信息
- 263、Ugly Number
- centos 装apache
- AutoLayout详解
- 【巩固地基】系列之:C#基础读书笔记(杂)
- javascript汇总(转)
- Redis命令总结
- android几种alertdialog
- jQuery多级手风琴菜单实例讲解
- Android布局优化经验总结
- 安卓模拟器安装时,SDK ROOT UNDEFINED ,问题解决方案
- 一行数据过长,自动隐藏一部分数据样式
- C语言的枚举类型
- beego内置的模板函数
- C++各大有名科学计算库(转)
- MFC架构
- 基于Facebook Buck改造Android构建系统之初体验
- 调试BUG二三事
- 在 Servlet 中,如果 parameter 的值不存在, getParameter 的返回值
- Android ActionBar完全解析(续)