java poi导出excel单元格设置自定义背景颜色

一、思考过程(看代码的移步第二点)

现有方法

现有资料多为使用IndexedColors设置颜色,

style.setFillForegroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);

但是IndexedColors能设置的颜色有限

IndexedColors.AQUA.getIndex() //1
IndexedColors.AUTOMATIC.getIndex() //2
IndexedColors.BLUE.getIndex() //3
IndexedColors.BLUE_GREY.getIndex() //4
IndexedColors.BRIGHT_GREEN.getIndex() //5
IndexedColors.BROWN.getIndex() //6
IndexedColors.CORAL.getIndex() //7
IndexedColors.CORNFLOWER_BLUE.getIndex() //8
IndexedColors.DARK_BLUE.getIndex() //9
IndexedColors.DARK_GREEN.getIndex() //10
IndexedColors.DARK_RED.getIndex() //11
IndexedColors.DARK_TEAL.getIndex() //12
IndexedColors.DARK_YELLOW.getIndex() //13
IndexedColors.GOLD.getIndex() //14
IndexedColors.GREEN.getIndex() //15
IndexedColors.GREY_25_PERCENT.getIndex() //16
IndexedColors.GREY_40_PERCENT.getIndex() //17
IndexedColors.GREY_50_PERCENT.getIndex() //18
IndexedColors.GREY_80_PERCENT.getIndex() //19
IndexedColors.INDIGO.getIndex() //20
IndexedColors.LAVENDER.getIndex() //21
IndexedColors.LEMON_CHIFFON.getIndex() //22
IndexedColors.LIGHT_BLUE.getIndex() //23
IndexedColors.LEMON_CHIFFON.getIndex() //24
IndexedColors.LIGHT_BLUE.getIndex() //25
IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex() //26
IndexedColors.LIGHT_GREEN.getIndex() //27
IndexedColors.LIGHT_ORANGE.getIndex() //28
IndexedColors.LIGHT_TURQUOISE.getIndex() //29
IndexedColors.LIGHT_YELLOW.getIndex() //30
IndexedColors.LIME.getIndex() //31
IndexedColors.MAROON.getIndex() //32
IndexedColors.OLIVE_GREEN.getIndex() //33
IndexedColors.ORANGE.getIndex() //34
IndexedColors.ORCHID.getIndex() //35
IndexedColors.PALE_BLUE.getIndex() //36
IndexedColors.PINK.getIndex() //37
IndexedColors.PLUM.getIndex() //38
IndexedColors.RED.getIndex() //39
IndexedColors.ROSE.getIndex() //40
IndexedColors.ROYAL_BLUE.getIndex() //41
IndexedColors.SEA_GREEN.getIndex() //42
IndexedColors.SKY_BLUE.getIndex() //43
IndexedColors.TAN.getIndex() //44
IndexedColors.TEAL.getIndex() //45
IndexedColors.TURQUOISE.getIndex() //46
IndexedColors.VIOLET.getIndex() //47
IndexedColors.WHITE.getIndex() //48
IndexedColors.YELLOW.getIndex() //49```

在这里插入图片描述

,而需求中所要颜色都是花里胡哨的,需要真正的自定义;

而颜色的本质是rgb,所以只要我们能自己设置rgb的值就能获取任意想要的颜色了;

源码分析

在这里插入图片描述可以发现setFillForegroundColor有两个实现方法,但是究其本质,都是接收一个XSSFColor对象作为参数
在这里插入图片描述
其他的代码,看看就好,关键点就在XSSFColor这个对象上;
点进这个对象,看其结构,发现有一个setRGB()方法,接收参数为一个byte数组;
在这里插入图片描述
到这里大家应该都明白下面该怎么操作了吧,为避免有的朋友不知道,以下贴出我的步骤:

  • 获取想要颜色的rgb值:
    以上图为例,使用windows自带的画图工具打开,按下图顺序操作,比如我想获取这个粉色:
    在这里插入图片描述就可以获取到rgb值了。
    在这里插入图片描述

  • rgb值转为byte数组:
    主要用到两个方法:

    /**
    * rgb转int
    */
    private static int getIntFromColor(int Red, int Green, int Blue){
        Red = (Red << 16) & 0x00FF0000;
        Green = (Green << 8) & 0x0000FF00;
        Blue = Blue & 0x000000FF;
        return 0xFF000000 | Red | Green | Blue;
    }

    /**
     * int转byte[]
     */
    public static byte[] intToByteArray(int i) {
        byte[] result = new byte[4];
        result[0] = (byte)((i >> 24) & 0xFF);
        result[1] = (byte)((i >> 16) & 0xFF);
        result[2] = (byte)((i >> 8) & 0xFF);
        result[3] = (byte)(i & 0xFF);
        return result;
    }

得到byte数组后将其传入XSSFColor对象,此style的背景颜色就变为了你想要的颜色了。

二、示例代码

// 创建一个 workbook 对象 
Workbook workbook = new XSSFWorkbook();
// 创建一个 sheet对象
Sheet sheet = workbook.createSheet();
//创建一行对象
Row row = sheet.createRow((short) 1);
//获取样式对象
XSSFCellStyle = workbook.createCellStyle();
//自定义颜色对象
XSSFColor color = new XSSFColor();
//根据你需要的rgb值获取byte数组
color.setRGB(intToByteArray(getIntFromColor(255,255,255)));
//自定义颜色
style.setFillForegroundColor(color);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
Cell cell = row.createCell((short) 1);
cell.setCellValue("X1");
cell.setCellStyle(style);

/**
* rgb转int
*/
private static int getIntFromColor(int Red, int Green, int Blue){
    Red = (Red << 16) & 0x00FF0000;
    Green = (Green << 8) & 0x0000FF00;
    Blue = Blue & 0x000000FF;
    return 0xFF000000 | Red | Green | Blue;
}

/**
 * int转byte[]
 */
public static byte[] intToByteArray(int i) {
    byte[] result = new byte[4];
    result[0] = (byte)((i >> 24) & 0xFF);
    result[1] = (byte)((i >> 16) & 0xFF);
    result[2] = (byte)((i >> 8) & 0xFF);
    result[3] = (byte)(i & 0xFF);
    return result;
}

完结撒花~~~若文章有不对处,下面自行评论,博主很少上csdn

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐