Java poi 自定义RGB颜色
来源:互联网 发布:新生分班软件 编辑:程序博客网 时间:2024/05/17 04:50
最近有个需求,要求将数据插入创建的Excel中,并将第一行某些列背景颜色做自定义RGB,在网上找了一些,结合自己本身需求,写了一些,这是其中自己感觉比较方便的
用到的java尬包
import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFPalette;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.usermodel.Row;
public static void createExcel() { String toFilePath = "D:\\Excel\\" + "OOB.xls"; String sheetName = "oob.xls"; String[] titleRow = { "第一列", "第二列", "第三列", "第四列" }; // 创建workbook HSSFWorkbook workbook = new HSSFWorkbook(); // 添加Worksheet(不添加sheet时生成的xls文件打开时会报错) HSSFSheet sheet1 = workbook.createSheet(sheetName); FileOutputStream fo = null; BufferedOutputStream out = null; File file = new File(toFilePath); if (!file.exists()) { boolean mkdirs = file.mkdirs(); } if (new File(toFilePath).exists()) { new File(toFilePath).delete(); } try { fo = new FileOutputStream(toFilePath); out = new BufferedOutputStream(fo); // 添加表头 Row row = sheet1.createRow(0); // 创建第一行 for (int i = 0; i < titleRow.length; i++) { String string = titleRow[i]; HSSFCellStyle style = workbook.createCellStyle(); //设置字体加粗 HSSFFont font = workbook.createFont(); //粗体显示 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //设置字体格式 font.setFontName("宋体"); style.setFont(font); // 设置背景颜色IndexedColors.LIGHT_YELLOW.getIndex()这个是JAVA提供的,是一个short类型, //我们需要做的就是用我们自定义RGB将这个颜色替换掉 style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); //自定义RGB HSSFPalette customPalette = workbook.getCustomPalette(); //这里的setColorAtIndex方法需要的参数是(short index, byte red, byte green, byte blue) //这里的short我们直接用Java给我们提供的,我看有些人用的是自定义的short那个会有问题的 customPalette.setColorAtIndex(IndexedColors.LIGHT_YELLOW.getIndex(), (byte) 255, (byte) 230, (byte) 153); // 设置列宽 sheet1.setColumnWidth(i, 9000); // 创建第i列 HSSFCell cell = (HSSFCell) row.createCell(i); cell.setCellValue(titleRow[i]); //让设置生效 cell.setCellStyle(style); } fo = new FileOutputStream(toFilePath); out = new BufferedOutputStream(fo); workbook.write(out); } catch (Exception e) { e.printStackTrace(); } finally { try { out.flush(); } catch (IOException e) { e.printStackTrace(); } } }
以上就是设置某一列的自定义RGB的实现方法,新人第一次发表,求轻喷
阅读全文
0 0
- Java poi 自定义RGB颜色
- POI设置自定义的RGB背景颜色
- JAVA POI 自定义颜色
- java POI实现Excel单元格自定义颜色
- POI-----自定义颜色
- ios rgb颜色的自定义
- POI学习笔记 自定义颜色
- 【POI】自定义cell的颜色
- winfrom 控件背景颜色自定义RGB 值
- 【java】poi颜色对照表
- POI生成excel文件,自定义单元格颜色
- POI生成excel文件,自定义单元格颜色
- POI生成excel文件,自定义单元格颜色
- POI之Excel自定义单元格背景颜色
- POI 自定义16进制颜色导出
- MATLAB自定义函数:RGB与HSI颜色空间互换
- NPOI 自定义设置单元格背景颜色[RGB格式]
- C# winfrom label 控件背景颜色自定义RGB 值
- 小白学分布式程序开发12-PAXOS算法例子
- 一线程序猿的早上思考
- 解决vmware虚拟机克隆网络配置问题
- 博弈均衡算法
- es curator安装使用记录
- Java poi 自定义RGB颜色
- RA
- NodeJS+ExpressJS解决跨域请求CORS
- 代理模式之静态代理
- 微信小程序开发--表单验证
- java中使用json(org.json)
- 有关析构函数和构造函数的几点说明
- HDU6130 Kolakoski(模拟,2017 HDU多校联赛 第7场)
- HDU6127 Hard challenge 极角排序|暴力