1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > java poi导出excel单元格设置自定义背景颜色(任意颜色)

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

时间:2019-06-08 18:41:52

相关推荐

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

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

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

现有方法

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

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

但是IndexedColors能设置的颜色有限

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。