最近项目中用到了POI导出功能。以下技术供大家学习
完整代码如下:
CellStyle style = wb.createCellStyle();
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setBorderTop(CellStyle.BORDER_THIN);
// 居中
style.setAlignment(CellStyle.ALIGN_CENTER);
// **设置背景颜色,系统设置的48种颜色**(背景色)
// style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
// style.setFillPattern(CellStyle.SOLID_FOREGROUND);
/**
* 设置自定义颜色
*/
//取色板
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFPalette palette = workbook.getCustomPalette();
String Color = "#C7EDCC";
String r = Color.substring(1, 3);
String g = Color.substring(3, 5);
String b = Color.substring(5, 7);
int r2 = Integer.parseInt(r, 16);
int g2 = Integer.parseInt(g, 16);
int b2 = Integer.parseInt(b, 16);
HSSFColor hssfColor = palette.findSimilarColor(r2, g2, b2);
//设置自定义颜色(背景色)
style.setFillForegroundColor(hssfColor.getIndex());
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
Font font = wb.createFont();
font.setFontName("宋体");
// 设置字体大小
font.setFontHeightInPoints((short) 16);
//字体颜色
font.setColor(Font.COLOR_RED);;
// 加粗
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
return style;```
## 设置POi预定义的颜色
**设置poi设置的颜色比较简单**
/style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
poi定义的48种颜色代码:
IndexedColors.AQUA.getIndex();
IndexedColors.AUTOMATIC.getIndex();
IndexedColors.BLUE.getIndex();
IndexedColors.BLUE_GREY.getIndex();
IndexedColors.BRIGHT_GREEN.getIndex();
IndexedColors.BROWN.getIndex();
IndexedColors.CORAL.getIndex();
IndexedColors.CORNFLOWER_BLUE.getIndex();
IndexedColors.DARK_BLUE.getIndex();
IndexedColors.DARK_GREEN.getIndex();
IndexedColors.DARK_RED.getIndex();
IndexedColors.DARK_TEAL.getIndex();
IndexedColors.DARK_YELLOW.getIndex();
IndexedColors.GOLD.getIndex();
IndexedColors.GREEN.getIndex();
IndexedColors.GREY_25_PERCENT.getIndex();
IndexedColors.GREY_40_PERCENT.getIndex();
IndexedColors.GREY_50_PERCENT.getIndex();
IndexedColors.GREY_80_PERCENT.getIndex();
IndexedColors.INDIGO.getIndex();
IndexedColors.LAVENDER.getIndex();
IndexedColors.LEMON_CHIFFON.getIndex();
IndexedColors.LIGHT_BLUE.getIndex();
IndexedColors.LEMON_CHIFFON.getIndex();
IndexedColors.LIGHT_BLUE.getIndex();
IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex();
IndexedColors.LIGHT_GREEN.getIndex();
IndexedColors.LIGHT_ORANGE.getIndex();
IndexedColors.LIGHT_TURQUOISE.getIndex();
IndexedColors.LIGHT_YELLOW.getIndex();
IndexedColors.LIME.getIndex();
IndexedColors.MAROON.getIndex();
IndexedColors.OLIVE_GREEN.getIndex();
IndexedColors.ORANGE.getIndex();
IndexedColors.ORCHID.getIndex();
IndexedColors.PALE_BLUE.getIndex();
IndexedColors.PINK.getIndex();
IndexedColors.PLUM.getIndex();
IndexedColors.RED.getIndex();
IndexedColors.ROSE.getIndex();
IndexedColors.ROYAL_BLUE.getIndex();
IndexedColors.SEA_GREEN.getIndex();
IndexedColors.SKY_BLUE.getIndex();
IndexedColors.TAN.getIndex();
IndexedColors.TEAL.getIndex();
IndexedColors.TURQUOISE.getIndex();
IndexedColors.VIOLET.getIndex();
IndexedColors.WHITE.getIndex();
IndexedColors.YELLOW.getIndex();
poi定义的48种颜色效果如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200110142335915.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjY0ODY5Mg==,size_16,color_FFFFFF,t_70)
## 设置自定义颜色
自定义颜色需要调用取色板自定义颜色,然后才能使用此颜色
//取色板
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFPalette palette = workbook.getCustomPalette();
// 颜色的RGB代码:#C7EDCC,这里可以更换成你需要的颜色
String Color = “#C7EDCC”;
String r = Color.substring(1, 3);
String g = Color.substring(3, 5);
String b = Color.substring(5, 7);
int r2 = Integer.parseInt(r, 16);
int g2 = Integer.parseInt(g, 16);
int b2 = Integer.parseInt(b, 16);
HSSFColor hssfColor = palette.findSimilarColor(r2, g2, b2);
//设置自定义颜色(背景色)
style.setFillForegroundColor(hssfColor.getIndex());
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
本人创建的交流qun,欢迎加入:344-635-699