Java poi 自定义RGB颜色
2017-08-16 09:55
239 查看
最近有个需求,要求将数据插入创建的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的实现方法,新人第一次发表,求轻喷
相关文章推荐
- JAVA POI 自定义颜色
- POI设置自定义的RGB背景颜色
- java POI实现Excel单元格自定义颜色
- 自定义 : Html 颜色选择器,支持返回RGB值与Html颜色值
- Java 利用 ICC 色彩空间 color space profile 实现 RGB 和 CMYK 颜色的相互转换
- 自定义View之RGB颜色变化Paint画笔颜色过滤器setColorFilter,LightingColorFilter光照过滤器
- MATLAB自定义函数:RGB与HSI颜色空间互换
- RGB,CMY(K),YUV,YIQ,YCbCr颜色的转换算法(java实现) 分类: Android JAVA 2015-06-08 19:30 26人阅读 评论(0) 收藏
- POI生成excel文件,自定义单元格颜色
- POI如何自定义单元格背景颜色
- RGB,CMY(K),YUV,YIQ,YCbCr颜色的转换算法(java实现)
- java使用poi导出excel设置颜色问题
- POI生成excel文件,自定义单元格颜色
- 使用 Java 进行图像处理 - 取得图像上指定位置像素的 rgb 颜色分量
- java用JBarcode组件生成条形码(支持自定义字体及颜色),图文详解之2-1
- 滚动条实现RGB颜色的调制(窗体程序)--JAVA基础
- Java POI导出Excel时,字体单元格颜色示例
- 使用poi对excel条件格式设置字体颜色使用自定义的颜色
- Java 中RGB颜色自动生成 十六进制 Java代码
- POI之Excel自定义单元格背景颜色