easyExcel版本(2.0.4)
前提:不要问我为啥不升级版本,升级有对应功能注解,由于之前的代码都是用这个版本,如果改版本号可能会造成之前的功能异常,而项目时间紧急不好去动!!!
1. 自定义颜色注解
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelCellFront {
short FrontColor() default 10;
short FrontSize() default 14;
}
2.编写对应操作器
public class CustomFrontStrategy implements RowWriteHandler {
private Class<?> elementType;
public CustomFrontStrategy(Class<?> elementType) {
this.elementType = elementType;
}
private void setFront(Row row){
Class<?> eleType = this.elementType;
Field[] fields = eleType.getDeclaredFields();
for (Field field : fields) {
ExcelCellFront annotation = field.getAnnotation(ExcelCellFront.class);
if(ObjectUtils.isEmpty(annotation)){
continue;
}
ExcelProperty easyExcelAnnotation = field.getAnnotation(ExcelProperty.class);
if (null == easyExcelAnnotation) {
continue;
}
for (int i = 0; i < row.getLastCellNum(); i++) {
Cell cell = row.getCell(i);
if(cell.getStringCellValue().equals(easyExcelAnnotation.value()[1])){
Workbook workbook = cell.getSheet().getWorkbook();
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
WriteFont cellWriteFont = new WriteFont();
cellWriteFont.setColor(annotation.FrontColor());
cellWriteFont.setFontHeightInPoints(annotation.FrontSize());
contentWriteCellStyle.setWriteFont(cellWriteFont);
CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, contentWriteCellStyle);
cell.setCellStyle(cellStyle);
}
}
}
}
@Override
public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, int i, int i1, boolean b) {
}
@Override
public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, int i, boolean isHead) {
Sheet sheet = writeSheetHolder.getSheet();
if (isHead) {
Row titleRow = sheet.getRow(0);
titleRow.setHeight((short) 1800);
}
if(sheet.getLastRowNum() -1 == 1){
Row sheetRow = sheet.getRow(1);
setFront(sheetRow);
}
}
}
3.在对应字段头上添加注解
public class GradeStep1TemplateModel extends BaseRowModel {
@ExcelProperty(value = {"说明:测试1,3必填", "测试1"}, index = 0)
@ColumnWidth(30)
@ExcelCellFront(FrontColor = 10,FrontSize = 14)
@CustomMerge(needMerge = true, isPk = true)
private String test1;
@ExcelProperty(value = {"说明:测试1,3必填", "测试2"}, index = 1)
@ColumnWidth(40)
@CustomMerge(needMerge = true)
private String test2;
@ColumnWidth(40)
@CustomMerge(needMerge = true)
@ExcelCellFront(FrontColor = 10,FrontSize = 14)
@ExcelProperty(value = {"说明:测试1,3必填", "测试3"}, index = 2)
private String test3;
}
4.写入对应文件进行操作器注册
@Test
public void testExcel() {
String sheetName = "测试文件";
GradeStep1TemplateModel model = new GradeStep1TemplateModel();
model.setCpCode("111");
model.setCpName("111");
List<GradeStep1TemplateModel> modelList = Lists.newArrayList(model);
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
EasyExcel.write(bos, modelList.get(0).getClass())
.excelType(ExcelTypeEnum.XLSX)
.registerWriteHandler(new CustomMergeStrategy(GradeStep1TemplateModel.class))
.sheet(sheetName).doWrite(modelList);
FileOutputStream fileOutputStream = new FileOutputStream("/Users/hanli/vipsrv-logs/demo/test"+System.currentTimeMillis()+".xlsx");
fileOutputStream.write(bos.toByteArray());
} catch (Exception e) {
throw new CustomException(PerformanceResponseCode.EXPORT_EXCEL_WRONG_DATA);
}
}
}
5.最后效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/e4e6a847e424459389495a42afbd6699.png)