EasyExcel下载带下拉框和批注模板

EasyExcel下载带下拉框和批注模板

一、 代码实现

  1. 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();}}
  1. 实体对象
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;}
  1. 下拉框拦截器
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;}
}
  1. 批注拦截器
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);}}
}

二、实现效果

  1. 批注效果
    在这里插入图片描述
  2. 下拉框效果
    在这里插入图片描述

三、参考文档

easyExcel自定义拦截器

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/670939.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Mysql-数据库优化-客户端连接参数

客户端参数 原文地址 # 连接池配置 # 初始化连接数 spring.datasource.druid.initial-size1 # 最小空闲连接数&#xff0c;一般设置和initial-size一致 spring.datasource.druid.min-idle1 # 最大活动连接数&#xff0c;一个数据库能够支撑最大的连接数是多少呢&#xff1f; …

Vue中的请求拦截器

目录 1 前言 2 使用方法 2.1 创建拦截器 2.2 引入拦截器 1 前言 我们常常会使用JWT令牌来验证登录&#xff0c;因此很多请求都需要携带JWT令牌&#xff0c;我们当然可以用{headers:{Authorization:xx}}的方式&#xff0c;向每个请求中都以这样的方式添加JWT令牌。不过这样…

同样的代码,开发环境生效,测试环境不生效,记一次问题排查过程和原因分析

我的公众号:IT周瑜 今天有同事遇到这样一个问题,有一个新增接口在本地和开发环境的swagger上都能看到,但是在测试环境的swagger上却看不到,并且确定测试环境是最新的代码,并重新构建和部署了。 按照同事的描述,首先我得确定是不是swagger的问题,我先检查我们平台组是否…

javaEE - 23( 21000 字 Servlet 入门 -1 )

一&#xff1a;Servlet 1.1 Servlet 是什么 Servlet 是一种实现动态页面的技术. 是一组 Tomcat 提供给程序猿的 API, 帮助程序猿简单高效的开发一个 web app. 构建动态页面的技术有很多, 每种语言都有一些相关的库/框架来做这件事&#xff0c;Servlet 就是 Tomcat 这个 HTTP…

[第五天】C++继承:单继承、多继承、菱形继承和虚继承的深度解析

一、单继承 1、概述 C最重要的特征是代码重用&#xff0c;通过继承机制可以利用已有的数据类型来定义新的数据类型&#xff0c;新的类不仅拥有旧类的成员&#xff0c;还拥有新定义的成员。 例如一个B类继承于A类&#xff0c;或称从类A派生类B。这样的话&#xff0c;类A成为基类…

2月06日,每日信息差

第一、建业地产有40个项目入围“白名单”&#xff0c;项目分布在郑州、信阳、焦作、洛阳等城市。接近建业地产人士表示&#xff0c;因各城市项目申报、审批工作进度不同&#xff0c;各城市项目的“白名单”申报工作还在持续进行&#xff0c;相关审批通过后&#xff0c;公司入围…

零基础学Python之面向对象

1.面向对象编程简介 &#xff08;1&#xff09;什么是面向对象 面向对象程序设计(Object Oriented Programming)作为一种新方法&#xff0c;其本质是以建立模型体现出来的抽象思维过程和面向对象的方法。模型是用来反映现实世界中事物特征的。任何一个模型都不可能反映客观事…

StarRocks 1 月社区动态(2024)

StarRocks 在 2023 年不断进化&#xff0c;锤炼核心功能&#xff0c;打磨全新特性&#xff0c;成功实现了从全场景 OLAP 到云原生湖仓的全面升级&#xff0c;成为湖仓新范式的造浪者。2024 年&#xff0c;我们期待与您一同续写新篇章&#xff0c;共同迈向“极速统一”的新征程&…

使用#if 和 #endif 实现注释,及原因

注释我们代码中经常使用&#xff0c; 单行注释和多行注释我们都会经常用到。 但是有些场景下这两种注释并不是很方便&#xff0c;这样我们就可以使用上面的两个宏来对代码进行注释。 什么情况下使用那两种注释不方便? 1. 如果代码很多时&#xff0c;我们如果一行一行…

【Qt】常见问题

1.存在未解析的标识符 将build文件夹删掉重新编译。 2.左侧项目目录栏无法删除已添加项目 打开目标项目上一级的pro文件&#xff0c;将目标文件名字注释或者删除掉&#xff0c;最后保存&#xff0c;qt就会自动更新&#xff0c;将该项目隐藏掉。 3.在qt creator下添加槽函数…

c++运算符优先级

这里写目录标题 C运算符优先级定义了表达式中运算符的计算顺序。当一个表达式中有多个运算符时&#xff0c;运算符的优先级决定了哪些运算符先被计算。下面是C中一些常见运算符的优先级列表&#xff0c;从高到低排序&#xff1a; 阿里云参考链接&#xff1a;developer.aliyun.c…

模拟串口LV2,解决硬件串口资源不足问题!!!!

模拟串口通信 2.0 版本&#xff01;&#xff01; 我在前面的文章里面有写了 虚拟串口通信&#xff0c;虽然说能用&#xff0c;但是用过的小伙伴都说 “好!” 优缺点: 先说一点&#xff0c;2.0版本并不适用于同硬件串口的所有场合&#xff0c;仅仅针对自己开发的电子垃圾的主…

[office] 网优必备的10大经典函数公式! #知识分享#媒体

网优必备的10大经典函数公式! Excel软件看似简单&#xff0c;其实花样很多&#xff0c;尤其Excel表格。但其实只要用心多练&#xff0c;效率轻松提升个十倍百倍真不是问题!赶紧一起来get新技能吧~ ▋函数公式一 我们都知道从网管中查询出来的经纬度是没有小数点的。我看到不…

护眼灯色温多少合适?推荐五款合适色温的护眼台灯

很多人在购买台灯之后只会根据周围环境灯光的明暗调节亮度&#xff0c;对于色温的了解并不多&#xff0c;不知道色温应该调节到什么数值比较合适&#xff0c;有些人也根本没有意识到色温在影响人情绪方面起着重要作用&#xff0c;接下来就一起来看一下色温的标准。 一、什么色…

Day4.

单链表 #include <head.h>typedef struct List{int value;struct List *pointe; }*list; list create_space() {list s(struct List *)malloc(sizeof(struct List)); //向堆区申请空间s->pointe NULL;//初始化s->value 0;return s; } list inserhead_list(lis…

AI改编游戏大电影《使命召唤:幽灵重生》(下)

AI改编游戏大电影《使命召唤&#xff1a;幽灵重生》&#xff08;下&#xff09; 幽灵重生携生化武器毁灭人类&#xff0c;普莱斯上尉点上雪茄拿起武器&#xff0c;英雄再次迎来使命的召唤&#xff01; 《使命召唤&#xff1a;幽灵重生》&#xff08;下&#xff09;&#xff1a…

电力负荷预测 | 基于TCN的电力负荷预测(Python)———结果比对

文章目录 效果一览文章概述源码设计参考资料效果一览 文章概述 电力负荷预测 | 基于TCN的电力负荷预测(Python)———结果比对 python3.8 keras2.6.0 matplotlib3.5.2 numpy1.19.4 pandas1.4.3 tensorflow==2.6.0

【宝藏系列】嵌入式入门概念大全

【宝藏系列】嵌入式入门概念大全 0️⃣1️⃣操作系统&#xff08;Operating System&#xff0c;OS&#xff09; 是管理计算机硬件与软件资源的系统软件&#xff0c;同时也是计算机系统的内核与基石。操作系统需要处理管理与配置内存、决定系统资源供需的优先次序、控制输入与输…

jquery写表格,通过后端传值,并合并单元格

<!DOCTYPE html> <html> <head><title>Table Using jQuery</title><style>#tableWrapper {width: 100%;height: 200px; /* 设置表格容器的高度 */overflow: auto; /* 添加滚动条 */margin-top: -10px; /* 负的外边距值&#xff0c;根据实际…

Days 20 ElfBoard 板 FFmpeg移植

FFmpeg是一套可以用来记录、转换数字音频、视频&#xff0c;并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。因此&#xff0c;对于从事多媒体技术开发的工程师来说&#xff0c;深入研究FFMPEG成为一门必不可少的工作…