EasyExcel下载带下拉框和批注模板
EasyExcel下载带下拉框和批注模板
一、 代码实现
- controller下载入口
/***下载excel模板* @author youlu* @date 2023/8/14 17:31* @param response* @param request* @return void*/@PostMapping("/downloadTemplate")public void downloadExcel(HttpServletResponse response, HttpServletRequest request) throws IOException {//查询字典数据,用于模板下拉框和批注说明使用Map<String, List<SysDictData>> dictDataMap = dictDataService.selectDictDataMapByDictTypeAndStatus("worksheet", "0");//获取供应商类型,不同供应商类型展示的下拉框和批注会有不一样Boolean supplier = getSupplierBoolean();ParamThreadLocal.setParam(supplier);try {long currentTimeMillis = System.currentTimeMillis();String name = "工单模板_" + currentTimeMillis;response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setCharacterEncoding("utf-8");// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系String fileName = URLEncoder.encode(name, "UTF-8");response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), TWorkSheetReadVO.class).inMemory(true).registerWriteHandler(new CommentWriteHandler(dictDataMap)) //加下拉框的拦截器.registerWriteHandler(new CustomSheetWriteHandler(dictDataMap)) //加批注的拦截器.build();WriteSheet writeSheet = EasyExcel.writerSheet("工单模板").build();excelWriter.write(Lists.newArrayList(), writeSheet);excelWriter.finish();} finally {ParamThreadLocal.clearParam();}}
- 实体对象
package com.smy.ows.project.worksheet.domain.vo;import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.HeadStyle;
import com.alibaba.excel.converters.date.DateStringConverter;
import com.alibaba.excel.enums.poi.FillPatternTypeEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.smy.framework.base.DesensitizationAnnotation;
import com.smy.ows.project.worksheet.enums.SheetLevelEnums;
import com.smy.ows.project.worksheet.enums.WorkSheetStatus;
import com.smy.ows.util.*;
import lombok.Data;import java.io.Serializable;
import java.util.Date;/*** 客诉工单对象 t_work_sheet** @author youlu* @date 2023-01-11*/
@Data
public class TWorkSheetReadVO implements Serializable {private static final long serialVersionUID = 5924360788178861972L;/*** 客诉标题*/@ExcelProperty(value = "客诉标题", index = 0)@ColumnWidth(20)private String complaintHeadline;/*** @see SheetLevelEnums*/@ExcelProperty(value = "优先级", index = 1, converter = PriorityIntegerStringConverter.class)@ColumnWidth(10)@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)private Integer priority;@ExcelProperty(value = "客户姓名", index = 2)@ColumnWidth(20)@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)private String custName;/*** 客户号*/@ExcelProperty(value = "客户号", index = 3)@ColumnWidth(20)private String custNo;@DesensitizationAnnotation@ExcelProperty(value = "客户手机号", index = 4)@ColumnWidth(20)@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)private String custMobile;@DesensitizationAnnotation@ExcelProperty(value = "客户身份证", index = 5)@ColumnWidth(30)private String custIdNo;/*** 投诉时间*/@ExcelProperty(value = "投诉时间(yyyy-MM-dd HH:mm:ss)", index = 6, converter = DateStringConverter.class)@ColumnWidth(40)@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private Date complaintTime;//反馈渠道@ExcelProperty(value = "反馈渠道", index = 7, converter = ChannelStringStringConverter.class)@ColumnWidth(15)@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)private String feedbackChannel;@ExcelProperty(value = "工单类型", index = 8, converter = TypeIntegerStringConverter.class)@ColumnWidth(15)@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)private Integer type;@ExcelProperty(value = "业务类型", index = 9, converter = BizTypeIntegerStringConverter.class)@ColumnWidth(15)@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)private Integer bizType;@DesensitizationAnnotation@ExcelProperty(value = "客户联系方式", index = 10)@ColumnWidth(15)private String custContactMobile;/*** 所属资方*/@ExcelProperty(value = "所属资方", index = 11)@ColumnWidth(15)private String capital;@ExcelProperty(value = "投诉内容", index = 12)@ColumnWidth(30)@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)private String content;/*** @see WorkSheetStatus*/@ExcelProperty(value = "工单状态", index = 13, converter = StatusIntegerStringConverter.class)@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)@ColumnWidth(15)private Integer status;@ExcelProperty(value = "处理结果", index = 14, converter = ResultIntegerStringConverter.class)@ColumnWidth(15)private Integer result;/*** 处理情况*/@ExcelProperty(value = "处理情况", index = 15)@ColumnWidth(15)private String handingInfo;}
- 下拉框拦截器
package com.smy.ows.util;import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;
import com.smy.ows.common.core.domain.entity.SysDictData;
import com.smy.ows.common.utils.ParamThreadLocal;
import com.smy.ows.project.worksheet.constant.WorksheetDictTypeConstant;
import com.smy.ows.project.worksheet.enums.WorkSheetStatus;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.util.CellRangeAddressList;import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** 自定义拦截器.** @author youlu*/
public class CustomSheetWriteHandler implements SheetWriteHandler {private Map<String, List<SysDictData>> notationMap;public CustomSheetWriteHandler(Map<String, List<SysDictData>> notationMap) {this.notationMap = notationMap;}@Overridepublic void afterSheetCreate(SheetWriteHandlerContext context) {DataValidationHelper helper = context.getWriteSheetHolder().getSheet().getDataValidationHelper();Map<Integer, String[]> mapDropDown = this.getIntegerMap();for (Integer integer : mapDropDown.keySet()) {//起始行,结束行,元素位置(ExcelProperty中的value值)CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 65535, integer, integer);String[] strings = mapDropDown.get(integer);DataValidationConstraint constraint = helper.createExplicitListConstraint(strings);DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);context.getWriteSheetHolder().getSheet().addValidationData(dataValidation);}}private Map<Integer, String[]> getIntegerMap() {//map中key对应,ExcelProperty中的value值。map中value对应下拉框的值Map<Integer, String[]> mapDropDown = new HashMap<>();for (String key : notationMap.keySet()) {String[] strings = notationMap.get(key).stream().map(k -> k.getDictLabel()).toArray(String[]::new);if (WorksheetDictTypeConstant.WORKSHEET_RESULT.equals(key)) {mapDropDown.put(14, strings);} else if (WorksheetDictTypeConstant.WORKSHEET_TYPE.equals(key)) {mapDropDown.put(8, strings);} else if (WorksheetDictTypeConstant.WORKSHEET_BIZ_TYPE.equals(key)) {mapDropDown.put(9, strings);} else if (WorksheetDictTypeConstant.WORKSHEET_PRIORITY.equals(key)) {mapDropDown.put(1, strings);} else if (WorksheetDictTypeConstant.WORKSHEET_FEEDBACK_CHANNEL.equals(key)) {mapDropDown.put(7, strings);}}Boolean supplier = (Boolean) ParamThreadLocal.getParam();if (supplier) {//供应商 和 资方的,工单状态只能选择【待分配】mapDropDown.put(13, new String[]{WorkSheetStatus.PENDING.getDesc()});//其他的工单状态只能选择【待分配】和 【已处理】} else {mapDropDown.put(13, new String[]{WorkSheetStatus.PENDING.getDesc(), WorkSheetStatus.FINISHED.getDesc()});}return mapDropDown;}
}
- 批注拦截器
package com.smy.ows.util;import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
import com.google.common.collect.Lists;
import com.smy.ows.common.core.domain.entity.SysDictData;
import com.smy.ows.project.worksheet.constant.WorksheetDictTypeConstant;
import com.smy.ows.project.worksheet.enums.WorkSheetStatus;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;/*** 自定义拦截器.新增注释,第一行头加批注** @author Jiaju Zhuang*/
public class CommentWriteHandler implements RowWriteHandler {private final Map<String, List<SysDictData>> notationMap;public CommentWriteHandler(Map<String, List<SysDictData>> notationMap) {this.notationMap = notationMap;}@Overridepublic void afterRowDispose(RowWriteHandlerContext context) {if (BooleanUtils.isTrue(context.getHead())) {Sheet sheet = context.getWriteSheetHolder().getSheet();Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch();// 在第一行 第二列创建一个批注String priorityDesc = Optional.ofNullable(notationMap.get(WorksheetDictTypeConstant.WORKSHEET_PRIORITY)).orElse(Lists.newArrayList()).stream().map(k -> k.getDictValue() + ":" + k.getDictLabel()).collect(Collectors.joining("\r\n"));//对应要加批注的元素的ExcelProperty中的value值 Comment comment = drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short)1, 0, (short)2, 1));comment.setString(new XSSFRichTextString(priorityDesc));// 将批注添加到单元格对象中sheet.getRow(0).getCell(1).setCellComment(comment);//对应要加批注的元素的ExcelProperty中的value值Comment comment6 = drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short)6, 0, (short)2, 1));comment6.setString(new XSSFRichTextString("yyyy-MM-dd HH:mm:ss"));sheet.getRow(0).getCell(6).setCellComment(comment6);String channelDesc = Optional.ofNullable(notationMap.get(WorksheetDictTypeConstant.WORKSHEET_FEEDBACK_CHANNEL)).orElse(Lists.newArrayList()).stream().map(k -> k.getDictValue() + ":" + k.getDictLabel()).collect(Collectors.joining("\r\n"));Comment comment7 = drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) 7, 0, (short) 2, 1));comment7.setString(new XSSFRichTextString(channelDesc));sheet.getRow(0).getCell(7).setCellComment(comment7);String typeDesc = Optional.ofNullable(notationMap.get(WorksheetDictTypeConstant.WORKSHEET_TYPE)).orElse(Lists.newArrayList()).stream().map(k -> k.getDictValue() + ":" + k.getDictLabel()).collect(Collectors.joining("\r\n"));Comment comment8 = drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) 8, 0, (short) 2, 1));comment8.setString(new XSSFRichTextString(typeDesc));sheet.getRow(0).getCell(8).setCellComment(comment8);String bizDesc = Optional.ofNullable(notationMap.get(WorksheetDictTypeConstant.WORKSHEET_BIZ_TYPE)).orElse(Lists.newArrayList()).stream().map(k -> k.getDictValue() + ":" + k.getDictLabel()).collect(Collectors.joining("\r\n"));Comment comment9 = drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) 9, 0, (short) 2, 1));comment9.setString(new XSSFRichTextString(bizDesc));sheet.getRow(0).getCell(9).setCellComment(comment9);String statusDesc = Arrays.stream(WorkSheetStatus.values()).map(k -> k.getCode() + ":" + k.getDesc()).collect(Collectors.joining("\r\n"));Comment comment13 = drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) 13, 0, (short) 2, 1));comment13.setString(new XSSFRichTextString(statusDesc));sheet.getRow(0).getCell(13).setCellComment(comment13);String resultDesc = Optional.ofNullable(notationMap.get(WorksheetDictTypeConstant.WORKSHEET_RESULT)).orElse(Lists.newArrayList()).stream().map(k -> k.getDictValue() + ":" + k.getDictLabel()).collect(Collectors.joining("\r\n"));Comment comment14 = drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) 14, 0, (short) 2, 1));comment14.setString(new XSSFRichTextString(resultDesc));sheet.getRow(0).getCell(14).setCellComment(comment14);}}
}
二、实现效果
- 批注效果

- 下拉框效果

三、参考文档
easyExcel自定义拦截器
相关文章:
EasyExcel下载带下拉框和批注模板
EasyExcel下载带下拉框和批注模板 一、 代码实现 controller下载入口 /***下载excel模板* author youlu* date 2023/8/14 17:31* param response* param request* return void*/PostMapping("/downloadTemplate")public void downloadExcel(HttpServletResponse r…...
C语言之字符逆序(牛客网)
个人主页(找往期文章包括但不限于本期文章中不懂的知识点):我要学编程(ಥ_ಥ)-CSDN博客 字符逆序__牛客网 题目: 思路:既然有空格就不能用scanf函数来接收字符了。因为scanf函数遇到空格会停止读取。我们可以用get…...
RAPTOR:树组织检索的递归抽象处理
RAPTOR: RECURSIVE ABSTRACTIVE PROCESSING FOR TREE-ORGANIZED RETRIEVAL Title:树组织检索的递归抽象处理 https://arxiv.org/pdf/2401.18059.pdf 摘要 检索增强语言模型可以更好的融入长尾问题,但是现有的方法只检索短的连续块,限制了整…...
图论:合适的环
4979. 合适的环 - AcWing题库 给定一个 n 个点 m 条边的无向图。 图中不含重边和自环。 请你在图中选出一个由三个点组成的环。 设图中一共有 x 条边满足:不在选择的环内,且与选择的环内某个点相连。 我们希望通过合理选环,使得 x 的值尽可能…...
【数据分享】1929-2023年全球站点的逐月平均降水量(Shp\Excel\免费获取)
气象数据是在各项研究中都经常使用的数据,气象指标包括气温、风速、降水、湿度等指标,说到常用的降水数据,最详细的降水数据是具体到气象监测站点的降水数据! 有关气象指标的监测站点数据,之前我们分享过1929-2023年全…...
React+Antd实现省、市区级联下拉多选组件(支持只选省不选市)
1、效果 是你要的效果,咱们继续往下看,搜索面板实现省市区下拉,原本有antd的Cascader组件,但是级联组件必须选到子节点,不能只选省,满足不了页面的需求 2、环境准备 1、react18 2、antd 4 3、功能实现 …...
CentOS镜像如何下载?在VMware中如何安装?
一、问题 CentOS镜像如何下载?在VMware中如何安装? 二、解决 1、CentOS镜像的下载 (1)官方网站 The CentOS Project (2)官方中文官网 CentOS 中文 官网 (3)选择CentOS Linux…...
计算机科学导论(4)DMA传输原理
文章目录 DMA的工作原理DMA的优势DMA的类型DMA的应用 DMA(Direct Memory Access)直接内存访问是一种允许某些硬件子系统在不通过中央处理单元(CPU)的情况下,直接从内存读取或向内存写入数据的技术。这种方式可以显著提…...
select、poll和epoll的区别
文章目录 概要一、多路复用I/O模型的诞生1.1 多线程或进程方式1.2 通过数组,链表等方式保存socket fd,不断轮询 二、select三、poll四、epoll五、小结六、参考 概要 在Unix五种I/O模型一文中,提到了I/O多路复用模型,其在Linux下有…...
gpt今日最新新闻:gpts的广泛应用
最近,OpenAI给ChatGPT带来了一个备受期待的更新——“GPT提及(mentions)”功能。这项创新不仅增强了ChatGPT的实用性,也为AI在日常业务中的运用开辟了新路径。在本文中,我将分享我对这项新功能的初步体验,并…...
【进入游戏行业选游戏特效还是技术美术?】
进入游戏行业选游戏特效还是技术美术? 游戏行业正处于蓬勃发展的黄金时期,科技的进步推动了游戏技术和视觉艺术的飞速革新。在这个创意和技术挑战交织的领域里,游戏特效和技术美术岗位成为了许多人追求的职业目标。 这两个岗位虽然紧密关联…...
(delphi11最新学习资料) Object Pascal 学习笔记---第4章第2.3节(常量参数)
4.2.3 常量参数 作为引用参数的替代,您可以使用const参数。由于您无法在例程内为const参数赋予新值,因此编译器可以优化参数传递。编译器可以选择与引用参数相似的方法(或者在C术语中是const引用),但行为类似于值参…...
事件在状态流程图中的工作方式
什么是事件? 事件是一个Stateflow对象,它可以触发以下对象中一个动作: Simulink触发子系统 Simulink函数调用子系统 状态流程图 何时使用事件 当你想: 激活Simulink触发的子系统 激活Simulink函数调用子系统 在状态流程图…...
幻兽帕鲁能在Mac上运行吗?幻兽帕鲁Palworld新手攻略
幻兽帕鲁能在Mac上运行吗? 《幻兽帕鲁》目前还未正式登陆Mac平台,不过通过一些方法是可以让游戏在该平台运行的。 虽然游戏不能在最高配置下运行,但如果你安装了CrossOver这个软件,就可以玩了。这是为Mac、Linux和ChromeOS等设计…...
elementPlus实现动态表格单元格合并span-method方法总结
最近在做PC端需求的时候,需要把首列中相邻的同名称单元格合并。 我看了一下elementPlus官网中的table表格,span-method可以实现单元格合并。 我们先看一下官网的例子: 合并行或列 多行或多列共用一个数据时,可以合并行或列。 …...
视频上传 - 断点续传那点事
在上一篇文章中,我们讲解了分片上传的实现方式。在讲解断点续传之前,我要把上篇文章中留下的问题讲解一下。读过上一篇文章的小伙伴们都知道,对于分片上传来说,它的传输方式分为2种,一种是按顺序传输,一种是…...
Scala 和 Java在继承机制方面的区别
Scala 和 Java 都是面向对象编程语言,都支持类的继承机制。然而,尽管两者在基础概念上有很多相似之处,但在具体的实现和语法上,Scala 的继承机制有其独特之处。以下是 Scala 和 Java 在继承方面的一些主要区别: 多重继…...
spark sql上线前的调试工作实现
背景 每个公司应该都有大数据的平台的吧,平台的作用就是可以在上面执行各种spark sql以及定时任务,不过一般来说,由于这些spark sql的上线不经过测试,所以可能会影响到生产的数据,这种情况下大数据平台提供一个上线前…...
java -jar启动SpringBoot项目时配置文件加载位置与优先级
服务部署启动时,我们经常需要指定配置文件启动. 一般有四种,优先级如下 spring.config.location > spring.profiles.active > spring.config.additional-location > 默认的 application.yml 1.spring.config.location 外部配置文件优先级最高 一般配置文件在服务…...
每日一题 力扣LCP30.魔塔游戏
题目描述: 小扣当前位于魔塔游戏第一层,共有 N 个房间,编号为 0 ~ N-1。每个房间的补血道具/怪物对于血量影响记于数组 nums,其中正数表示道具补血数值,即血量增加对应数值;负数表示怪物造成伤害值&#x…...
应用升级/灾备测试时使用guarantee 闪回点迅速回退
1.场景 应用要升级,当升级失败时,数据库回退到升级前. 要测试系统,测试完成后,数据库要回退到测试前。 相对于RMAN恢复需要很长时间, 数据库闪回只需要几分钟。 2.技术实现 数据库设置 2个db_recovery参数 创建guarantee闪回点,不需要开启数据库闪回。…...
React hook之useRef
React useRef 详解 useRef 是 React 提供的一个 Hook,用于在函数组件中创建可变的引用对象。它在 React 开发中有多种重要用途,下面我将全面详细地介绍它的特性和用法。 基本概念 1. 创建 ref const refContainer useRef(initialValue);initialValu…...
黑马Mybatis
Mybatis 表现层:页面展示 业务层:逻辑处理 持久层:持久数据化保存 在这里插入图片描述 Mybatis快速入门 
CREATE USER read_only WITH PASSWORD 密码 -- 连接到xxx数据库 \c xxx -- 授予对xxx数据库的只读权限 GRANT CONNECT ON DATABASE xxx TO read_only; GRANT USAGE ON SCHEMA public TO read_only; GRANT SELECT ON ALL TABLES IN SCHEMA public TO read_only; GRANT EXECUTE O…...
CMake 从 GitHub 下载第三方库并使用
有时我们希望直接使用 GitHub 上的开源库,而不想手动下载、编译和安装。 可以利用 CMake 提供的 FetchContent 模块来实现自动下载、构建和链接第三方库。 FetchContent 命令官方文档✅ 示例代码 我们将以 fmt 这个流行的格式化库为例,演示如何: 使用 FetchContent 从 GitH…...
【HTTP三个基础问题】
面试官您好!HTTP是超文本传输协议,是互联网上客户端和服务器之间传输超文本数据(比如文字、图片、音频、视频等)的核心协议,当前互联网应用最广泛的版本是HTTP1.1,它基于经典的C/S模型,也就是客…...
QT: `long long` 类型转换为 `QString` 2025.6.5
在 Qt 中,将 long long 类型转换为 QString 可以通过以下两种常用方法实现: 方法 1:使用 QString::number() 直接调用 QString 的静态方法 number(),将数值转换为字符串: long long value 1234567890123456789LL; …...
均衡后的SNRSINR
本文主要摘自参考文献中的前两篇,相关文献中经常会出现MIMO检测后的SINR不过一直没有找到相关数学推到过程,其中文献[1]中给出了相关原理在此仅做记录。 1. 系统模型 复信道模型 n t n_t nt 根发送天线, n r n_r nr 根接收天线的 MIMO 系…...
基于Java+MySQL实现(GUI)客户管理系统
客户资料管理系统的设计与实现 第一章 需求分析 1.1 需求总体介绍 本项目为了方便维护客户信息为了方便维护客户信息,对客户进行统一管理,可以把所有客户信息录入系统,进行维护和统计功能。可通过文件的方式保存相关录入数据,对…...
【C++特殊工具与技术】优化内存分配(一):C++中的内存分配
目录 一、C 内存的基本概念 1.1 内存的物理与逻辑结构 1.2 C 程序的内存区域划分 二、栈内存分配 2.1 栈内存的特点 2.2 栈内存分配示例 三、堆内存分配 3.1 new和delete操作符 4.2 内存泄漏与悬空指针问题 4.3 new和delete的重载 四、智能指针…...
