package jnpf.excel; import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams; import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFColor; public class ExcelExportStyler implements IExcelExportStyler { private static final short FONT_SIZE_TWELVE = 12; private Workbook workbook; private CellStyle headStyle; private XSSFCellStyle cellStyle; public ExcelExportStyler(Workbook workbook) { this.workbook = workbook; this.headStyle = getBaseCellStyle(); this.cellStyle = (XSSFCellStyle) workbook.createCellStyle(); } @Override public CellStyle getHeaderStyle(short color) { CellStyle style = this.headStyle; return style; } @Override public CellStyle getTitleStyle(short color) { XSSFCellStyle style = (XSSFCellStyle) this.headStyle; style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true)); // 自定义背景颜色 byte[] rgb = new byte[]{(byte) 221, (byte) 220, (byte) 223}; // RGB值 XSSFColor customColor = new XSSFColor(rgb, new DefaultIndexedColorMap()); style.setFillForegroundColor(customColor); //设置填充模式为实心填充 style.setFillPattern(FillPatternType.SOLID_FOREGROUND); return style; } public void setBlackBorder(CellStyle style) { // 设置边框样式为细线 style.setBorderBottom(BorderStyle.THIN); style.setBorderLeft(BorderStyle.THIN); style.setBorderTop(BorderStyle.THIN); style.setBorderRight(BorderStyle.THIN); // 设置单元格颜色 style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 单元格内容水平居中 style.setAlignment(HorizontalAlignment.LEFT); // 单元格内容垂直居中 style.setVerticalAlignment(VerticalAlignment.CENTER); // 单元格内的文本超出单元格宽度时自动换行 style.setWrapText(true); } @Override public CellStyle getStyles(boolean parity, ExcelExportEntity entity) { XSSFCellStyle style = cellStyle; this.setBlackBorder(style); // 单元格内容水平居中 style.setAlignment(HorizontalAlignment.LEFT); // 单元格内容垂直居中 style.setVerticalAlignment(VerticalAlignment.CENTER); // 单元格内的文本超出单元格宽度时自动换行 style.setWrapText(true); return style; } @Override public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) { return getStyles(true, entity); } @Override public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) { return null; } /** * 表头单元格样式 * * @return */ private CellStyle getBaseCellStyle() { CellStyle style = workbook.createCellStyle(); this.setBlackBorder(style); // 单元格内容水平居中 style.setAlignment(HorizontalAlignment.CENTER); return style; } /** * 字体修改 * * @param size 字号 * @param isBold 加粗 * @return */ private Font getFont(Workbook workbook, short size, boolean isBold) { Font font = workbook.createFont(); font.setFontName("宋体"); font.setBold(isBold); font.setFontHeightInPoints(size); return font; } }