使用POI设置excel背景色

技术交流群:233513714

HSSFCellStyle setBorder1 = workbook.createCellStyle();
HSSFFont font1 = workbook.createFont();
font1.setFontName("Arial");
font1.setFontHeightInPoints((short) 14);//设置字体大小
setBorder1.setFont(font1);//选择需要用到的字体格式
setBorder1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
setBorder1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
setBorder1.setFillForegroundColor(IndexedColors.YELLOW.getIndex());  //设置前景色

//setBorder1.setFillForegroundColor(HSSFColor.RED.index);  //设置背景色

HSSFSheet sheet = workbook.createSheet("list");

getCell(sheet, i, 0).setCellValue("日期");
getCell(sheet, i, 0).setCellStyle(setBorder1);//设置样式

这里需要特别注意的是在设置背景色和前景色的时候一定要加上setBorder1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);这行代码,否则设置颜色不起作用

原文地址:https://www.cnblogs.com/cnndevelop/p/5590177.html