POI设置自定义的RGB背景颜色

来源:互联网 发布:网络菜市场 编辑:程序博客网 时间:2024/05/17 21:57

提出问题

POI如何自定义单元格背景颜色???

解决问题

例一:具体的看注释

import org.apache.poi.hssf.usermodel.*;import org.apache.poi.hssf.util.HSSFColor;import java.io.FileOutputStream;/** * Created by Ay on 2016/4/29. */public class PoiBackgroundColorTest {public static void main(String[] args) throws Exception{    //创建一份    HSSFWorkbook excel = new HSSFWorkbook();    //创建第一个sheet    HSSFSheet sheet = excel.createSheet("我的POI之旅");    //创建第一行    HSSFRow row = sheet.createRow((short) 0);    //创建第一个单元格    HSSFCell cell = row.createCell((short) 0);    //设置单元格的值    cell.setCellValue("Ay");    //生成单元格样式    HSSFCellStyle style = excel.createCellStyle();    //设置背景颜色    style.setFillForegroundColor(HSSFColor.LIME.index);    //solid 填充  foreground  前景色    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);    cell.setCellStyle(style);    //通过流写到硬盘    FileOutputStream out = new FileOutputStream("D:/old_color.xls");    excel.write(out);    out.close();    //======  这里是重点,马上要自定义单元格的样式了  =============    cell.setCellValue("Al");    //拿到palette颜色板    HSSFPalette palette = excel.getCustomPalette();    //这个是重点,具体的就是把之前的颜色 HSSFColor.LIME.index    //替换为  RGB(51,204,204) 宝石蓝这种颜色    //你可以改为 RGB(0,255,127)    palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 0, (byte) 255, (byte) 127);    //======  这里是重点,马上要自定义单元格的样式了  =============    out = new FileOutputStream("D:/new_color.xls");    excel.write(out);    out.close();}

}

D:/old_color.xls 结果 
这里写图片描述

D:/new_color.xls 结果 
这里写图片描述

原创粉丝点击