一、填充颜色
HSSFWorkbook workbook = new HSSFWorkbook();HSSFCellStyle style = workbook.createCellStyle();//设置单元格颜色style.setFillForegroundColor(HSSFColor.LIME.index));//设置填充样式(实心填充),不设置填充样式不会有颜色style.setFillPattern(FillPatternType.SOLID_FOREGROUND);//当然 style可以设置多个 填充时选取自己需要的就行//下面简单举例一下(单个单元格)其他的都一样HSSFSheet sheet = workbook.createSheet("excel");HSSFRow row = sheet.createRow(0);HSSFCell cell = row.createCell(0);cell.setCellValue("绿黄色"); //单元格内容cell.setCellStyle(style);//填充
二、POI内置所有颜色
三、所有内置颜色生成代码
Workbook wb = new SXSSFWorkbook();Sheet sheet = wb.createSheet("全部颜色");Map<Integer,HSSFColor> allColorMap = HSSFColor.getIndexHash();int rowNum = 0;for(Entry<Integer, HSSFColor> entry : allColorMap.entrySet()) {Row row = sheet.createRow(rowNum++);Cell cell = row.createCell(0);cell.setCellValue(entry.getValue().getClass().getSimpleName());Cell cellColor = row.createCell(1);CellStyle style = wb.createCellStyle();style.setFillPattern(FillPatternType.SOLID_FOREGROUND);style.setFillForegroundColor(entry.getValue().getIndex());cellColor.setCellStyle(style);}FileOutputStream fout = new FileOutputStream("G:/poi颜色大全.xlsx");wb.write(fout);wb.close();fout.close();
四、POI自定义颜色
当然,有些客户比较刁钻,内置的颜色满足不了时,就只能使用自定义颜色了;
import java.awt.Color;XSSFColor green = new XSSFColor(new Color(198,224,180));SXSSFWorkbook wb = new SXSSFWorkbook();XSSFCellStyle cellStyle = (XSSFCellStyle)workbook.createCellStyle();cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);cellStyle.setFillForegroundColor(green);
注意
Color
为java.awt.Color