之前在开发系统的导出功能时需要对单元格增加不同的样式,过程有点曲折,记录一下以备后续用到
1 2 3 4 5
| <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.2.1</version> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| try (ExcelWriter excelWriter = EasyExcel.write(filePath).build()) { WriteSheet sheet = EasyExcel.writerSheet("自定义样式") .head(ExportHeaderDTO.class) .useDefaultStyle(Boolean.FALSE) .registerWriteHandler(new CustomCellWriteHandler()) .registerWriteHandler(CustomHorizontalCellStyleStrategy.cellBorder()) .build(); excelWriter.write(collect, sheet); } catch (Exception e) { e.printStackTrace(); }
|
- 自定义样式 CustomCellWriteHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| public class CustomCellWriteHandler implements CellWriteHandler {
@Override public int order() { return 60000; }
@Override public void afterCellDispose(CellWriteHandlerContext context) { Cell cell = context.getCell(); if (BooleanUtils.isNotTrue(context.getHead())) { Workbook workbook = context.getWriteWorkbookHolder().getWorkbook(); XSSFCellStyle cellStyle = (XSSFCellStyle) workbook.createCellStyle(); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setTopBorderColor(IndexedColors.BLACK.index); cellStyle.setBottomBorderColor(IndexedColors.BLACK.index); cellStyle.setLeftBorderColor(IndexedColors.BLACK.index); cellStyle.setRightBorderColor(IndexedColors.BLACK.index); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); cellStyle.setAlignment(HorizontalAlignment.CENTER); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); Font font = workbook.createFont(); if (cell.getColumnIndex() == 1) { font.setColor(IndexedColors.DARK_TEAL.index); cellStyle.setFont(font); cellStyle.setFillForegroundColor(new XSSFColor(ColorConstant.CustomColor.PINK, CustomIndexedColorMap.fromColors(CTColors.Factory.newInstance()))); cell.setCellStyle(cellStyle); context.getFirstCellData().setWriteCellStyle(null); } } } }
|
- 边框样式 CustomHorizontalCellStyleStrategy.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class CustomHorizontalCellStyleStrategy extends HorizontalCellStyleStrategy { @Override public int order() { return 6500; }
public static HorizontalCellStyleStrategy cellBorder() { WriteCellStyle headWriteCellStyle = new WriteCellStyle(); headWriteCellStyle.setTopBorderColor(IndexedColors.BLACK.index); headWriteCellStyle.setBottomBorderColor(IndexedColors.BLACK.index); headWriteCellStyle.setLeftBorderColor(IndexedColors.BLACK.index); headWriteCellStyle.setRightBorderColor(IndexedColors.BLACK.index); headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND); WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); contentWriteCellStyle.setBorderRight(BorderStyle.THIN); contentWriteCellStyle.setBorderTop(BorderStyle.THIN); contentWriteCellStyle.setBorderRight(BorderStyle.THIN); contentWriteCellStyle.setBorderBottom(BorderStyle.THIN); return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); } }
|