Easyexcel3.2.1版本,设置单元格样式,公式,字体颜色,自定义标题 您所在的位置:网站首页 设置单元格颜色 Easyexcel3.2.1版本,设置单元格样式,公式,字体颜色,自定义标题

Easyexcel3.2.1版本,设置单元格样式,公式,字体颜色,自定义标题

2024-07-17 12:21| 来源: 网络整理| 查看: 265

文章目录 项目背景注意事项项目技术概要 重要的事情说三遍!!!效果图新建Handler自定义excel标题新建导出实体类重写beforeCellCreateController层调用,注册Handler 设置标题样式,包含字体,背景设置内容公式,内容字体样式,背景样式内容部分全部代码公式部分说明文本说明 所有代码Handler层 controller调用 小结致谢

项目背景

未经本人允许,不允许搬运!!!

最近接了一单企业的项目,需要频繁的对数据进行一个excel的整理,由于他们文档,工作步骤繁琐,不允许我们抛弃他们的工作方式(如果抛弃,会导致工作培训周期长,学习慢的困境),来进行一个每日统计报表管理的一个功能。需要严格按照他们的excel文档模板进行导出。

注意事项

在重写过程中,setHeadCellStyle,setContentCellStyle必须要一起实现才可以完成效果!!

项目技术 Springboot 3jdk 17easyexcel 3.2.1 概要

网上大部分资料都是关于easyexcel2.0版本的知识,对于3版本以上的资料都非常得少,而且资料内容质量参差不齐,我在这里给大家总结一个比较全面的关于3版本的资料,关于如何对excel背景样式,excel字体样式,公式,以及自定义标题做一个详细的攻略,我可以给兄弟们保证,以后用我这套,准能解决你的关于easyexcel的所有问题。 easyexcel的官方文档地址easyexcel官网文档

重要的事情说三遍!!!

本人有公司团队(现有多个业务),可代接各种vue项目,小程序,机器视觉,大模型,缺陷检测!!!可私信联系我!!! 本人有公司团队(现有多个业务),可代接各种vue项目,小程序,机器视觉,大模型,缺陷检测!!!可私信联系我!!! 本人有公司团队(现有多个业务),可代接各种vue项目,小程序,机器视觉,大模型,缺陷检测!!!可私信联系我!!!

效果图

按照我的代码,你做不出来这种效果的话,请直接来砍我 在这里插入图片描述 支持公式下拉 在这里插入图片描述

新建Handler public class ReportExportCellWriteHandler extends AbstractVerticalCellStyleStrategy { } 自定义excel标题 新建导出实体类

在这当中 ${title} 非常重要,这是我们自己定义的一种规则,我们会去匹配它,来进行一个替换的操作。官方文档中,并没有提及如何进行一个动态的标题导入,都是在controller层,新建一个List来保存标题,恕我直言!!这并不符合我们程序员对于代码通用性的一个要求。下面是我的解决方案。

package com.busin.system.model.vo; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.annotation.write.style.ColumnWidth; import lombok.Data; import java.math.BigDecimal; import java.util.Date; /** * @author shumm * 报表导出视图对象 * @since 2024-03-07 15:48 */ @Data @ColumnWidth(20) public class HongsReportExportVO { @DateTimeFormat("yyyy/MM/dd HH:mm:ss") @ExcelProperty(value = {"${title}", "出票日期"}) @ColumnWidth(20) private Date issueTickets; @ExcelProperty(value = {"${title}", "订单号/PNR"}) @ColumnWidth(10) private String flightOrder; @ExcelProperty(value = {"${title}", "票号"}) @ColumnWidth(10) private String issueTicketNumber; @ExcelProperty(value = {"${title}", "人数"}) @ColumnWidth(10) private Integer number; @ExcelProperty(value = {"${title}", "航程"}) @ColumnWidth(10) private String voyage; @ExcelProperty(value = {"${title}", "乘机日期"}) @ColumnWidth(10) private String boardinDate; @ExcelProperty(value = {"${title}", "航班号"}) @ColumnWidth(10) private String flightNumber; @ExcelProperty(value = {"${title}", "舱位"}) @ColumnWidth(10) private String shippingSpace; @ExcelProperty(value = {"${title}", "票面"}) @ColumnWidth(10) private BigDecimal parvalue; @ExcelProperty(value = {"${title}", "机燃"}) @ColumnWidth(10) private BigDecimal mechaniCalcomBustion; @ExcelProperty(value = {"${title}", "票面总价"}) @ColumnWidth(10) private BigDecimal faceFee; @ExcelProperty(value = {"${title}", "代理费"}) @ColumnWidth(10) private BigDecimal agencyFee; @ExcelProperty(value = {"${title}", "支付票款"}) @ColumnWidth(10) private BigDecimal ticketPay; @ExcelProperty(value = {"${title}", "延时/改名费"}) @ColumnWidth(10) private BigDecimal nameChangeFee; @ExcelProperty(value = {"${title}", "保险/行李"}) @ColumnWidth(10) private BigDecimal luggage; @ExcelProperty(value = {"${title}", "支付总额"}) @ColumnWidth(10) private BigDecimal paymentAmount; @ExcelProperty(value = {"${title}", "服务费"}) @ColumnWidth(10) private BigDecimal serviceFee; @ExcelProperty(value = {"${title}", "实际利润"}) @ColumnWidth(10) private BigDecimal actualProfit; @ExcelProperty(value = {"${title}", "应收款"}) @ColumnWidth(10) private BigDecimal accountsReceivable; @ExcelProperty(value = {"${title}", "已收款"}) @ColumnWidth(10) private BigDecimal receivedPayment; @ExcelProperty(value = {"${title}", "未收款"}) @ColumnWidth(10) private BigDecimal unpaidPayments; @ExcelProperty(value = {"${title}", "出票代理"}) @ColumnWidth(10) private String ticketingAgent; @ExcelProperty(value = {"${title}", "出票账号"}) @ColumnWidth(10) private String ticketAccount; @ExcelProperty(value = {"${title}", "客户名称"}) @ColumnWidth(10) private String customerName; @ExcelProperty(value = {"${title}", "备注"}) @ColumnWidth(10) private String remark; } 重写beforeCellCreate

在这里会替换掉我们自定义的${title}标题

public class ReportExportCellWriteHandler extends AbstractVerticalCellStyleStrategy { private String title; PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper("${", "}"); public ReportExportCellWriteHandler(String title) { this.title = title; } @Override public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) { if (head != null) { List headNameList = head.getHeadNameList(); if (CollectionUtils.isNotEmpty(headNameList)) { Properties properties = new Properties(); properties.setProperty("title", title); headNameList.replaceAll(value -> propertyPlaceholderHelper.replacePlaceholders(value, properties)); } } } } Controller层调用,注册Handler

在这里进行一个注册,这样就实现了动态导入标题的一个功能,根本不需要在controller层写入多余的代码,根本不需要!

/** * 导出报表 * @param response * @throws IOException */ @Log(title = "导出报表",businessType = BusinessType.EXPORT, isSaveResponseData = false,isSaveRequestData = false) @Operation(summary = "导出报表") @GetMapping("/export") public void exportHongsReports(HongsReportQuery queryParams,HttpServletResponse response) throws IOException { String fileName = "报表.xlsx"; response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); List exportHongsReportList = hongsReportService.listExportHongsReportExports(queryParams); EasyExcel.write(response.getOutputStream(), HongsReportExportVO.class) .registerWriteHandler(new ReportExportCellWriteHandler(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"))+" 机票销售日报表")) .sheet("报表") .doWrite(exportHongsReportList); } 设置标题样式,包含字体,背景

注意!!!这里只需要重写setHeadCellStyle函数即可,不要重写其他函数,可能会被覆盖,这是我弄了好几天才弄明白的地方,如果不信,可以去试一下,但是切记,设置标题,只要重写这一个方法即可,其他方法不要去重写! 必须要用WriteFont去重写字体,在官网文档中只介绍了如何修改背景颜色,在这里,我们可以做到只需要重写setHeadCellStyle即可完成所有标题的样式

@Override protected void setHeadCellStyle(CellWriteHandlerContext context) { // 获取和创建CellStyle WriteCellData cellData = context.getFirstCellData(); CellStyle originCellStyle = cellData.getOriginCellStyle(); Cell cell = context.getCell(); if (Objects.isNull(originCellStyle)) { originCellStyle = context.getWriteWorkbookHolder().getWorkbook().createCellStyle(); } ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 255, 255), new DefaultIndexedColorMap())); if(cell.getColumnIndex() == 10 || cell.getColumnIndex() == 12 || cell.getColumnIndex() == 15 || cell.getColumnIndex() == 17 || cell.getColumnIndex() == 18 || cell.getColumnIndex() == 20 ) { ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(237, 237, 237), new DefaultIndexedColorMap())); } // 设置背景颜色 originCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 重点!!! 由于在FillStyleCellWriteHandler,会把OriginCellStyle和WriteCellStyle合并,会已WriteCellStyle样式为主,所有必须把WriteCellStyle设置的背景色清空 // 具体合并规则请看WriteWorkbookHolder.createCellStyle方法 WriteCellStyle writeCellStyle = cellData.getWriteCellStyle(); writeCellStyle.setFillForegroundColor(null); // 重点!!! 必须设置OriginCellStyle cellData.setOriginCellStyle(originCellStyle); WriteFont headWriteFont = new WriteFont(); if (cell.getRowIndex() == 0) { headWriteFont.setFontHeightInPoints((short)10); headWriteFont.setFontName("Arial"); headWriteFont.setBold(true); }else if (cell.getRowIndex() == 1) { headWriteFont.setFontHeightInPoints((short)10); headWriteFont.setFontName("Arial"); headWriteFont.setBold(false); } cellData.getWriteCellStyle().setWriteFont(headWriteFont); } 设置内容公式,内容字体样式,背景样式

这里也是一样的,只需要重写setContentCellStyle,不需要重写其他方法,如果你重写了其他方法,请一定要删除,会影响这个函数,所有的内容都可以这setContentCellStyle中完成操作!

内容部分全部代码 @Override protected void setContentCellStyle(CellWriteHandlerContext context) { // 获取和创建CellStyle WriteCellData cellData = context.getFirstCellData(); CellStyle originCellStyle = cellData.getOriginCellStyle(); Cell cell = context.getCell(); if (Objects.isNull(originCellStyle)) { originCellStyle = context.getWriteWorkbookHolder().getWorkbook().createCellStyle(); } // 设置背景颜色 ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 192, 0), new DefaultIndexedColorMap())); System.out.println("进入第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "列数据..."); if (cell.getRowIndex() >= 2){ ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(237, 237, 237), new DefaultIndexedColorMap())); // 票面总价 if (cell.getColumnIndex() == 10){ int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("I" + actualCellRowNum +"+J" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "票面总价写入公式完成"); }else if (cell.getColumnIndex() == 12){ // 支付票款 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("K" + actualCellRowNum +"-L" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "支付票款写入公式完成"); }else if (cell.getColumnIndex() == 15){ // 支付总额 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("M" + actualCellRowNum +"+N" + actualCellRowNum +"+O" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "支付总额写入公式完成"); }else if (cell.getColumnIndex() == 17){ // 实际利润 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("L" + actualCellRowNum +"+Q" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "实际利润写入公式完成"); }else if (cell.getColumnIndex() == 18){ // 应收款 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("P" + actualCellRowNum +"+R" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "应收款写入公式完成"); }else if (cell.getColumnIndex() == 20){ // 未收款 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("S" + actualCellRowNum +"-T" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "未收款写入公式完成"); }else if (cell.getColumnIndex() >= 2){ ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(189, 215, 238), new DefaultIndexedColorMap())); }else if (cell.getColumnIndex() ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(237, 237, 237), new DefaultIndexedColorMap())); // 票面总价 if (cell.getColumnIndex() == 10){ int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("I" + actualCellRowNum +"+J" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "票面总价写入公式完成"); }else if (cell.getColumnIndex() == 12){ // 支付票款 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("K" + actualCellRowNum +"-L" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "支付票款写入公式完成"); }else if (cell.getColumnIndex() == 15){ // 支付总额 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("M" + actualCellRowNum +"+N" + actualCellRowNum +"+O" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "支付总额写入公式完成"); }else if (cell.getColumnIndex() == 17){ // 实际利润 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("L" + actualCellRowNum +"+Q" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "实际利润写入公式完成"); }else if (cell.getColumnIndex() == 18){ // 应收款 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("P" + actualCellRowNum +"+R" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "应收款写入公式完成"); }else if (cell.getColumnIndex() == 20){ // 未收款 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("S" + actualCellRowNum +"-T" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "未收款写入公式完成"); }else if (cell.getColumnIndex() >= 2){ ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(189, 215, 238), new DefaultIndexedColorMap())); }else if (cell.getColumnIndex() private String title; PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper("${", "}"); public ReportExportCellWriteHandler(String title) { this.title = title; } @Override public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) { if (head != null) { List headNameList = head.getHeadNameList(); if (CollectionUtils.isNotEmpty(headNameList)) { Properties properties = new Properties(); properties.setProperty("title", title); headNameList.replaceAll(value -> propertyPlaceholderHelper.replacePlaceholders(value, properties)); } } } @Override protected void setHeadCellStyle(CellWriteHandlerContext context) { // 获取和创建CellStyle WriteCellData cellData = context.getFirstCellData(); CellStyle originCellStyle = cellData.getOriginCellStyle(); Cell cell = context.getCell(); if (Objects.isNull(originCellStyle)) { originCellStyle = context.getWriteWorkbookHolder().getWorkbook().createCellStyle(); } ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 255, 255), new DefaultIndexedColorMap())); if(cell.getColumnIndex() == 10 || cell.getColumnIndex() == 12 || cell.getColumnIndex() == 15 || cell.getColumnIndex() == 17 || cell.getColumnIndex() == 18 || cell.getColumnIndex() == 20 ) { ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(237, 237, 237), new DefaultIndexedColorMap())); } // 设置背景颜色 originCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 重点!!! 由于在FillStyleCellWriteHandler,会把OriginCellStyle和WriteCellStyle合并,会已WriteCellStyle样式为主,所有必须把WriteCellStyle设置的背景色清空 // 具体合并规则请看WriteWorkbookHolder.createCellStyle方法 WriteCellStyle writeCellStyle = cellData.getWriteCellStyle(); writeCellStyle.setFillForegroundColor(null); // 重点!!! 必须设置OriginCellStyle cellData.setOriginCellStyle(originCellStyle); WriteFont headWriteFont = new WriteFont(); if (cell.getRowIndex() == 0) { headWriteFont.setFontHeightInPoints((short)10); headWriteFont.setFontName("Arial"); headWriteFont.setBold(true); }else if (cell.getRowIndex() == 1) { headWriteFont.setFontHeightInPoints((short)10); headWriteFont.setFontName("Arial"); headWriteFont.setBold(false); } cellData.getWriteCellStyle().setWriteFont(headWriteFont); } @Override protected void setContentCellStyle(CellWriteHandlerContext context) { // 获取和创建CellStyle WriteCellData cellData = context.getFirstCellData(); CellStyle originCellStyle = cellData.getOriginCellStyle(); Cell cell = context.getCell(); if (Objects.isNull(originCellStyle)) { originCellStyle = context.getWriteWorkbookHolder().getWorkbook().createCellStyle(); } // 设置背景颜色 ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 192, 0), new DefaultIndexedColorMap())); System.out.println("进入第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "列数据..."); if (cell.getRowIndex() >= 2){ ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(237, 237, 237), new DefaultIndexedColorMap())); // 票面总价 if (cell.getColumnIndex() == 10){ int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("I" + actualCellRowNum +"+J" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "票面总价写入公式完成"); }else if (cell.getColumnIndex() == 12){ // 支付票款 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("K" + actualCellRowNum +"-L" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "支付票款写入公式完成"); }else if (cell.getColumnIndex() == 15){ // 支付总额 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("M" + actualCellRowNum +"+N" + actualCellRowNum +"+O" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "支付总额写入公式完成"); }else if (cell.getColumnIndex() == 17){ // 实际利润 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("L" + actualCellRowNum +"+Q" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "实际利润写入公式完成"); }else if (cell.getColumnIndex() == 18){ // 应收款 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("P" + actualCellRowNum +"+R" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "应收款写入公式完成"); }else if (cell.getColumnIndex() == 20){ // 未收款 int actualCellRowNum = context.getCell().getRowIndex() + 1; context.getCell().setCellFormula("S" + actualCellRowNum +"-T" + actualCellRowNum); System.out.println("第" + context.getCell().getRowIndex() + "行,第" + context.getCell().getColumnIndex() + "未收款写入公式完成"); }else if (cell.getColumnIndex() >= 2){ ((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(new java.awt.Color(189, 215, 238), new DefaultIndexedColorMap())); }else if (cell.getColumnIndex() String fileName = "报表.xlsx"; response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); List exportHongsReportList = hongsReportService.listExportHongsReportExports(queryParams); EasyExcel.write(response.getOutputStream(), HongsReportExportVO.class) .registerWriteHandler(new ReportExportCellWriteHandler(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"))+" 机票销售日报表")) .sheet("报表") .doWrite(exportHongsReportList); } 小结

在重写过程中,setHeadCellStyle,setContentCellStyle必须要一起实现才可以完成效果!! 兄弟们,切记,严格按我这样做就行了,不要去重写其他方法,会有冲突!!!!!而且不要在controller层中,去写什么该死的List来设置标题样式了,这个绝对保证通用性。

致谢

在这里,感谢评论区博主 又是重名了 给我的补充 该博主的博客链接 博主补充链接



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有