irpas技术客

EasyExcel--自定义表头(标题)样式策略_猿于生活_easyexcel自定义表头样式

大大的周 4438

对具体表格设置策略, 具体到每一个表格,比如(边框,背景颜色,字体样式等等) 复杂表头样式对象【用于存储当表头的自定义样式信息】: /** * 复杂表头样式信息,包含需要自定义的表头坐标及样式 * */ public class ComplexHeadStyles { /** * 表头横坐标 - 行 * */ private Integer x; /** * 表头纵坐标 - 列 * */ private Integer y; /** * 内置颜色 * */ private Short indexColor; /** * 边框样式 */ private BorderStyle borderStyle; public ComplexHeadStyles(Integer x, Integer y, Short indexColor,BorderStyle borderStyle){ this.x=x; this.y=y; this.indexColor=indexColor; this.borderStyle=borderStyle; } public ComplexHeadStyles(Integer x, Integer y, BorderStyle borderStyle){ this.x=x; this.y=y; this.borderStyle=borderStyle; } private void setCroods(Integer x,Integer y){ this.x=x; this.y=y; } public Integer getX() { return x; } public void setX(Integer x) { this.x = x; } public Integer getY() { return y; } public void setY(Integer y) { this.y = y; } public Short getIndexColor() { return indexColor; } public void setIndexColor(Short indexColor) { this.indexColor = indexColor; } } 自定义样式策略: package gl.com.gugong.util; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.util.StyleUtil; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.style.AbstractCellStyleStrategy; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Workbook; import java.util.concurrent.ArrayBlockingQueue; public class HeadStyleWriteHandler extends AbstractCellStyleStrategy { /** * 复杂表头自定义样式队列,先进先出,方便存储 * */ private ArrayBlockingQueue<ComplexHeadStyles> headStylesQueue; /** * WorkBoot * */ private Workbook workbook; /** * 构造方法,创建对象时传入需要定制的表头信息队列 * */ public HeadStyleWriteHandler(ArrayBlockingQueue<ComplexHeadStyles> headStylesQueue){ this.headStylesQueue=headStylesQueue; } @Override protected void initCellStyle(Workbook workbook) { // 初始化信息时,保存Workbook对象,转换时需要使用 this.workbook=workbook; } @Override protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) { WriteCellStyle writeCellStyle=new WriteCellStyle(); if(headStylesQueue !=null && ! headStylesQueue.isEmpty()){ ComplexHeadStyles complexHeadStyle=headStylesQueue.peek(); // 取出队列中的自定义表头信息,与当前坐标比较,判断是否相符 if(cell.getColumnIndex() == complexHeadStyle.getY() && relativeRowIndex.equals(complexHeadStyle.getX())){ // 设置自定义的表头颜色 writeCellStyle.setFillForegroundColor(complexHeadStyle.getIndexColor()); // 设置自定义边框 writeCellStyle.setBorderTop(complexHeadStyle.getBorderStyle()); writeCellStyle.setBorderBottom(complexHeadStyle.getBorderStyle()); writeCellStyle.setBorderLeft(complexHeadStyle.getBorderStyle()); writeCellStyle.setBorderRight(complexHeadStyle.getBorderStyle()); //重要: 想设置其他东西,可以在加....!!!!!!!!!!!!!!!!!!!!!!! // 样式出队 headStylesQueue.poll(); } } // WriteCellStyle转换为CellStyle CellStyle headCellStyle = StyleUtil.buildHeadCellStyle(workbook, writeCellStyle); // 设置表头样式 cell.setCellStyle(headCellStyle); } @Override protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) { } } Excel写对象类 import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.annotation.write.style.HeadRowHeight; import com.study.poi.utils.SexConverterForStudentInfo; import lombok.Data; import java.time.LocalDateTime; /** * 学生信息导出-复杂表头-特殊表头样式【单独设置表头样式】(Excel类) * */ @Data @HeadRowHeight(20) @ColumnWidth(25) @ContentRowHeight(20) public class StudyPoiComplexHeadStyleWriteExportDTO { /** * 学号ID,主键ID * note 复杂表头使用这样的方式即可 * note 复杂表头无法使用注解【@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 14)】来达到满意的效果,其设定的样式会把学生基本信息和学号都设置为同样的颜色,无法单独设置,需另行处理 */ @ExcelProperty({"学生基本信息", "学号"}) private Long studentId; /** * 学生姓名 */ @ExcelProperty({"学生基本信息", "姓名"}) private String studentName; /** * 出生日期 */ @ExcelProperty(value = "出生日期") private String studentBirthday; /** * 性别 */ @ExcelProperty(value = "性别") private String studentSex; /** * 年级 */ @ExcelProperty(value = "年级") private Integer studentGrade; /** * 班级 */ @ExcelProperty(value = "班级") private Integer studentClass; } 写Excel /** * 复杂表头-自定义表头样式导出-学生信息表 * */ @PostMapping("/complexHeadStyleExportStudentInfo") public void complexHeadStyleExportStudentInfo(HttpServletResponse response) throws IOException{ try { // 查询导出的学生信息表数据 List<StudyPioStudentDTO> studyPioStudents=studentInfoExportService.searchAllStudentInfo(); // 字符编码 String encode="utf-8"; // 文件名 String fileName=URLEncoder.encode("复杂表头-自定义表头样式导出",encode).replaceAll("\\+","%20"); // response三部曲 1.设置响应文件类型 2.设置响应编码 3.设置响应文件拓展名 response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding(encode); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); // 设置表头样式队列【先进先出】 ArrayBlockingQueue<ComplexHeadStyles> complexHeadStylesArrayBlockingQueue=new ArrayBlockingQueue<>(4); /** * (0,0)和(0,1)位置的单元格设置背景色为红色;(1,0)设置为绿色;(1,1)设置为蓝色 * 写Excel是一行一行写的,因此入队顺序是这样 */ complexHeadStylesArrayBlockingQueue.add(new ComplexHeadStyles(0,0,IndexedColors.RED1.getIndex())); complexHeadStylesArrayBlockingQueue.add(new ComplexHeadStyles(0,1,IndexedColors.RED1.getIndex())); complexHeadStylesArrayBlockingQueue.add(new ComplexHeadStyles(1,0,IndexedColors.LIGHT_GREEN.getIndex())); complexHeadStylesArrayBlockingQueue.add(new ComplexHeadStyles(1,1,IndexedColors.SKY_BLUE.getIndex())); // 自定义表头策略 HeadStyleWriteHandler headStyleWriteHandler=new HeadStyleWriteHandler(complexHeadStylesArrayBlockingQueue); // 写Excel EasyExcelFactory.write(response.getOutputStream(), StudyPoiComplexHeadStyleWriteExportDTO.class) .registerWriteHandler(headStyleWriteHandler) .autoCloseStream(true) .sheet("自定义学生信息表头颜色") .doWrite(studyPioStudents); }catch (Exception e){ errorReturn(response); } } 导出效果:


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #对具体表格设置策略 #PUBLIC #class