设置单元格背景色:
// 设置背景色 XSSFCellStyle headersStyleCell = workbook.createCellStyle(); XSSFColor color = new XSSFColor(java.awt.Color.YELLOW, new DefaultIndexedColorMap()); headersStyleCell.setFillForegroundColor(color); XSSFRow row = sheet.createRow(index); XSSFCell cell = row.createCell(0); cell.setCellStyle(headersStyleCell);
创建单元格样式:
/** * 创建单元格标题样式 * @param workbook * @return */ private static CellStyle createCellTitleStyle(Workbook workbook) { CellStyle style = workbook.createCellStyle(); // 设置边框样式 style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); //设置对齐样式 style.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 生成字体 Font font = workbook.createFont(); // 表头样式 style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); // 把字体应用到当前的样式 style.setFont(font); return style; }
/** * 创建单元格正文样式 * * @param workbook 工作薄 */ private static CellStyle createCellContentStyle(Workbook workbook) { CellStyle style = workbook.createCellStyle(); // 设置边框样式 style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); //设置对齐样式 style.setAlignment(XSSFCellStyle.ALIGN_CENTER); //自动换行 style.setWrapText(true); // 生成字体 Font font = workbook.createFont(); // 正文样式 style.setFillPattern(XSSFCellStyle.NO_FILL); style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 style.setFont(font); return style; } /** * 单元格样式(Integer)列表 */ private static CellStyle createCellContent4IntegerStyle(Workbook workbook) { CellStyle style = workbook.createCellStyle(); // 设置边框样式 style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); //设置对齐样式 style.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 生成字体 Font font = workbook.createFont(); // 正文样式 style.setFillPattern(XSSFCellStyle.NO_FILL); style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 style.setFont(font); style.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0")); //数据格式只显示整数 return style; } /** * 单元格样式(Double)列表 */ private static CellStyle createCellContent4DoubleStyle(Workbook workbook) { CellStyle style = workbook.createCellStyle(); // 设置边框样式 style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); //设置对齐样式 style.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 生成字体 Font font = workbook.createFont(); // 正文样式 style.setFillPattern(XSSFCellStyle.NO_FILL); style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 style.setFont(font); style.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00")); //保留两位小数点 return style; } /** * 单元格样式列表 */ private static Map < String, CellStyle > styleMap(Workbook workbook) { Map < String, CellStyle > styleMap = new LinkedHashMap <String, CellStyle > (); styleMap.put("head", createCellHeadStyle(workbook)); styleMap.put("title", createCellTitleStyle(workbook)); styleMap.put("content", createCellContentStyle(workbook)); styleMap.put("integer", createCellContent4IntegerStyle(workbook)); styleMap.put("double", createCellContent4DoubleStyle(workbook)); return styleMap; }