当前位置: 首页 > news >正文

easyExcel合并单元格导出

一、导入maven依赖

(很多旧项目自定义了一套Excel导出工具,poi版本可能不兼容,一般poi新旧版本不兼容分界线在3.17,选择3.17版本不会发生代码不兼容情况)

        <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.2.6</version><exclusions><exclusion><groupId>org.apache.poi</groupId><artifactId>poi</artifactId></exclusion><exclusion><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId></exclusion><exclusion><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version><classifier>sources</classifier></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-collections4</artifactId><version>4.1</version></dependency>

二、重写easyExcel-自定义写入处理器


import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;import java.util.ArrayList;
import java.util.List;/*** 自定义合并策略 该类继承了AbstractMergeStrategy抽象合并策略,需要重写merge()方法* @author reshui* @date 2023/9/4**/
public class CustomMergeStrategy extends AbstractMergeStrategy {/*** 分组,每几行合并一次*/private List<Integer> exportFieldGroupCountList;/*** 目标合并列index*/private Integer targetColumnIndex;/*** 需要开始合并单元格的首行index*/private Integer rowIndex;/*** @param exportDataList exportDataList为待合并目标列的值* @param targetColumnIndex 需要合并的列* @return {@code  }* @author reshui* @date 2023/09/05*/public CustomMergeStrategy(List<String> exportDataList, Integer targetColumnIndex) {this.exportFieldGroupCountList = getGroupCountList(exportDataList);this.targetColumnIndex = targetColumnIndex;}@Overrideprotected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {if (null == rowIndex) {rowIndex = cell.getRowIndex();}// 仅从首行以及目标列的单元格开始合并,忽略其他if (cell.getRowIndex() == rowIndex && cell.getColumnIndex() == targetColumnIndex) {mergeGroupColumn(sheet);}}private void mergeGroupColumn(Sheet sheet) {int rowCount = rowIndex;for (Integer count : exportFieldGroupCountList) {if (count == 1) {rowCount += count;continue;}// 合并单元格CellRangeAddress cellRangeAddress = new CellRangeAddress(rowCount, rowCount + count - 1, targetColumnIndex, targetColumnIndex);sheet.addMergedRegionUnsafe(cellRangeAddress);rowCount += count;}}/*** 该方法将目标列根据值是否相同连续可合并,存储可合并的行数* @param exportDataList* @return {@code List<Integer> }* @author reshui* @date 2023/09/05*/private List<Integer> getGroupCountList(List<String> exportDataList) {if (CollectionUtils.isEmpty(exportDataList)) {return new ArrayList<>();}List<Integer> groupCountList = new ArrayList<>();int count = 1;for (int i = 1; i < exportDataList.size(); i++) {if (exportDataList.get(i).equals(exportDataList.get(i - 1))) {count++;} else {groupCountList.add(count);count = 1;}}// 处理完最后一条后groupCountList.add(count);return groupCountList;}}

三、导出工具类封装

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;/*** 下载excel文件工具** @author reshui* @date 2023/09/05*/
public class DownloadExcelUtil {private static final Logger log = LoggerFactory.getLogger(DownloadExcelUtil.class);private final static String separatorChar = "-";public static <T> void downloadFile(HttpServletResponse response, Class<T> clazz, String data) throws IOException {String timeStamp = DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss");String fileName = timeStamp + "-log";downloadFile(response, clazz, data, fileName, "数据", null);}/*** @param response         响应请求* @param clazz            导出数据类型* @param data             数据源* @param customFileName   文件名* @param sheetName        页名* @param writeHandlerList 自定义写入处理器* @return {@code T }* @author reshui* @date 2023/09/05*/public static <T> T downloadFile(HttpServletResponse response, Class<T> clazz, String data, String customFileName, String sheetName, List<WriteHandler> writeHandlerList) throws IOException {// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postmantry {response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setCharacterEncoding("utf-8");// 这里URLEncoder.encode可以防止中文乱码 当然和easy-excel没有关系String fileName = URLEncoder.encode(customFileName, "UTF-8").replaceAll("\\+", "%20");response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");// 这里需要设置不关闭流ExcelWriterSheetBuilder writerSheetBuilder = EasyExcel.write(response.getOutputStream(), clazz).autoCloseStream(Boolean.FALSE).sheet(sheetName);if (CollUtil.isNotEmpty(writeHandlerList)) {for (WriteHandler writeHandler : writeHandlerList) {writerSheetBuilder.registerWriteHandler(writeHandler);}}writerSheetBuilder.doWrite(JSONObject.parseArray(data, clazz));} catch (Exception e) {// 重置responseresponse.reset();response.setContentType("application/json");response.setCharacterEncoding("utf-8");Map<String, String> map = new HashMap<>(2);map.put("status", "failure");map.put("message", "下载文件失败" + e.getMessage());response.getWriter().println(JSON.toJSONString(map));}return null;}/*** 出把excel** @param timeStamp       时间戳* @param excelFileName   excel文件名字* @param headClassType   头类类型* @param resultExcelList 结果excel表* @param filePath        文件路径* @author reshui* @date 2023/02/15*/public static void outputExcelToLocal(String timeStamp, String excelFileName, Class headClassType, List resultExcelList, String filePath) {//文件时间戳timeStamp = Objects.nonNull(timeStamp) ? timeStamp : StrUtil.EMPTY;String partFileName = filePath + File.separator + excelFileName + separatorChar + timeStamp + "-log.xlsx";FileUtil.touch(partFileName);EasyExcel.write(partFileName, headClassType).sheet("源数据").doWrite(resultExcelList);}/*** 简单把excel** @param excelFileName   excel文件名字* @param headClassType   头类类型* @param resultExcelList 结果excel表* @param filePath        文件路径* @author reshui* @date 2023/02/15*/public static void easyOutputExcelToLocal(String excelFileName, Class headClassType, List resultExcelList, String filePath) {String timeStamp = DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss");outputExcelToLocal(timeStamp, excelFileName, headClassType, resultExcelList, filePath);}public static void main(String[] args) {String timeStamp = DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss");String titleTmpDirPath = FileUtil.getTmpDirPath() + File.separator + "test" + File.separator + timeStamp + File.separator;try {System.out.println("日志存储地址-[" + titleTmpDirPath + "]");log.info("日志存储[" + titleTmpDirPath + "]");String partFileName = titleTmpDirPath + timeStamp + "-log.xlsx";FileUtil.touch(partFileName);EasyExcel.write(partFileName, String.class).sheet("源数据").doWrite(null);} catch (Exception e) {log.error("日志存储[" + titleTmpDirPath + "]" + "-error:", e);}}
}

四、运行

@RestController
@RequestMapping("/check")
public class TestController {@RequestMapping(value = "/run1",method = {RequestMethod.GET})public void run1(HttpServletResponse response) throws IOException {List<DemoData> demoDataList = data1();List<WriteHandler> customMergeStrategies = Arrays.asList(new CustomMergeStrategy(demoDataList.stream().map(DemoData::getName).collect(Collectors.toList()), 1));DownloadExcelUtil.downloadFile(response,DemoData.class, JSONObject.toJSONString(demoDataList),"测试","页1", customMergeStrategies);}public static List<DemoData> data1(){List<DemoData> demoDataList = new ArrayList<>();DemoData demoData0 = new DemoData();demoData0.setId("0");demoData0.setName("hello0");demoDataList.add(demoData0);DemoData demoData1 = new DemoData();demoData1.setId("1");demoData1.setName("hello1");demoDataList.add(demoData1);DemoData demoData11 = new DemoData();demoData11.setId("1");demoData11.setName("hello1");demoDataList.add(demoData11);DemoData demoData2 = new DemoData();demoData2.setId("2");demoData2.setName("hello2");demoDataList.add(demoData2);return demoDataList;}
}

相关文章:

easyExcel合并单元格导出

一、导入maven依赖 &#xff08;很多旧项目自定义了一套Excel导出工具&#xff0c;poi版本可能不兼容&#xff0c;一般poi新旧版本不兼容分界线在3.17&#xff0c;选择3.17版本不会发生代码不兼容情况&#xff09; <dependency><groupId>com.alibaba</groupId&…...

SpringBoot项目--电脑商城【用户注册】

1.创建数据表 1.1 创建t_user表 CREATE TABLE t_user (uid INT AUTO_INCREMENT COMMENT 用户id,username VARCHAR(20) NOT NULL UNIQUE COMMENT 用户名,password CHAR(32) NOT NULL COMMENT 密码,salt CHAR(36) COMMENT 盐值,phone VARCHAR(20) COMMENT 电话号码,email VARCH…...

HCIP学习-IPv6

目录 前置学习内容 IPv6解决的一些IPv4的缺陷 无限的地址 层次化的地址结构 即插即用 简化报文头部 IPv4和IPv6报头比较 端到端的网络罗完整性 安全性增强 挣钱QoS特性 IPv6地址介绍 格式 首选格式 压缩格式 内嵌IPv4地址格式的IPv6地址格式 IPv6的网络前缀和接…...

golang高精度十进制数扩展包decimal用法

在Go语言中&#xff0c;没有内置的十进制数&#xff08;decimal&#xff09;类型或相关的标准库。然而&#xff0c;有一些第三方包可用于处理十进制数&#xff0c;其中比较常用的是decimal包。 decimal包提供了一个big.Float的子类型decimal.Decimal&#xff0c;可以用于表示和…...

STM32F4X RNG随机数发生器

STM32F4X RNG随机数发生器 随机数的作用STM32F4X 随机数发生器RNG控制寄存器RNG状态寄存器RNG数据寄存器RNG数据步骤RNG例程 随机数的作用 随机数顾名思义就是随机产生的数字&#xff0c;这种数字最大的特点就是其不确定性&#xff0c;你不知道它下一次产生的数字是什么。随机…...

5、QT中SQLite数据库的操作

一、QT中的SQLite数据库 1、添加头文件和模块 Header: #include <QSqlDatabase> qmake: QT sql//pro文件添加sql模块执行数据库操作的类&#xff1a; Header: #include <QSqlQuery> qmake: QT sql2、C语言中的SQLite增删减查 SQLite3的基础教程 3、SQLite的…...

git回退到某个提交

git是一个分布式版本控制软件&#xff0c;分布式版本库的做法使源代码的发布和交流都极为方便&#xff0c;因此有不少用户都在使用git。最近小编也正在学习git这款软件&#xff0c;发现要想熟练运用git&#xff0c;学会git中的一些命令是很重要的&#xff0c;如果我们要回滚到某…...

对可再生能源和微电网集成研究的新控制技术和保护算法进行基线和测试及静态、时域和频率分析研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…...

Full authentication is required to access this resource解决办法

我们在使用postman调接口时候&#xff0c;有的时候需要权限才可以访问&#xff0c;否则可能会报下面这个错误 {"success": false,"message": "Full authentication is required to access this resource","code": 401,"result&q…...

Jetty:使用上下文文件部署离线瓦片.md

说明 介绍利用jetty在任意位置如桌面的资源进行发布。比如下载的离线瓦片数据&#xff0c;如果放到jetty的webapps目录下&#xff0c;则启动时间会比较久&#xff0c;可以通过本文的步骤进行配置&#xff0c;也免去了拷贝过程的耗时。 关键字&#xff1a;自定义路径、Jetty、…...

Docker实战:docker compose 搭建Rocketmq

1、配置文件准备 1.1、 新建目录&#xff1a;/home/docker/data/rocketmq/conf mkdir /home/docker/data/rocketmq/conf1.2、 在上面目录下新建文件broker.conf文件&#xff0c;内容如下 brokerClusterName DefaultCluster brokerName broker-a brokerId 0 deleteWhen 0…...

STL常用容器 (C++核心基础教程之STL容器详解)String的API

在C的标准模板库&#xff08;STL&#xff09;中&#xff0c;有多种容器可供使用。以下是一些常见的容器类型&#xff1a; 序列容器&#xff08;Sequential Containers&#xff09;&#xff1a; std::vector&#xff1a;动态数组&#xff0c;支持快速随机访问。 std::list&…...

《人生苦短,我学Python》——条件判断->(if-elif-else)多向选择 条件嵌套

今天&#xff0c;我们来学习多向选择&#xff01;if--elif--else if 后的语句是当 if 判断条件成立时&#xff0c;执行的操作。elif 后的语句是当 if 判断不成立时&#xff0c;再判断一次&#xff0c;如果成立&#xff0c;执行的操作。else 后的语句是当以上所有判断条件都不成…...

MongoDB 数据库性能优化技巧

原文&#xff1a;MongoDB 数据库性能优化技巧 (techdatafuture.com) MongoDB 是一款灵活且可扩展的NoSQL数据库&#xff0c;为了提高其性能&#xff0c;我们可以采取一些优化技巧。本文将介绍一些MongoDB性能优化的关键点&#xff0c;包括索引的使用、查询优化、数据模型设计和…...

网络安全人才缺口超百万,如今的就业情况怎样?

网络安全人才缺口超百万&#xff0c;如今的就业情况怎样&#xff1f; 2022年9月7日&#xff0c;国家网络安全宣传周准时开始。本次网络安全宣传周和以前一样&#xff0c;主要目的都是为了普及网络安全知识&#xff0c;提高网络安全的防护技能&#xff0c;提升对电信网络诈骗的…...

「MySQL」MySQL面试题全解析:常见问题与高级技巧详解

MySQL面试题全解析&#xff1a;常见问题与高级技巧详解 1. 什么是数据库&#xff1f;2. 什么是MySQL&#xff1f;3. 什么是SQL&#xff1f;4. 什么是主键&#xff1f;5. 什么是外键&#xff1f;6. 请解释索引是什么以及为什么使用索引&#xff1f;7. 什么是事务&#xff1f;8. …...

【USRP】产品型号、参数、架构全解析系列 6:N320 / N321

一、USRP 简介 通用软件无线电外设( USRP ) 是由 Ettus Research 及其母公司National Instruments设计和销售的一系列软件定义无线电。USRP 产品系列由Matt Ettus领导的团队开发&#xff0c;被研究实验室、大学和业余爱好者广泛使用。 大多数 USRP 通过以太网线连接到主机&am…...

Apifox 常用 JS 脚本

前置脚本常用 1、时间戳生成&#xff08;秒级&#xff09;&#xff1a; // 1、生成秒级时间戳到全局变量中 //let timestamp parseInt(new Date().getTime() / 1000) //pm.globals.set(timestamp, timestamp) // 2、生成秒级时间戳到全局变量中 pm.globals.set(timestamp, p…...

防止SQL注入的四种方案

一、什么是SQL注入&#xff1f; SQL注入即是指web应用程序对用户输入数据的合法性没有判断或过滤不严&#xff0c;攻击者可以在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句&#xff0c;在管理员不知情的情况下实现非法操作&#xff0c;以此来实现欺骗数据库服…...

java单元测试

版本区别 特性Junit 4Junit 5在当前类的所有测试方法之前执行。注解在静态方法上。此方法可以包含一些初始化代码。BeforeClassBeforeAll在当前类中的所有测试方法之后执行。注解在静态方法上。此方法可以包含一些清理代码。AfterClassAfterAll在每个测试方法之前执行。注解在…...

Leetcode 3576. Transform Array to All Equal Elements

Leetcode 3576. Transform Array to All Equal Elements 1. 解题思路2. 代码实现 题目链接&#xff1a;3576. Transform Array to All Equal Elements 1. 解题思路 这一题思路上就是分别考察一下是否能将其转化为全1或者全-1数组即可。 至于每一种情况是否可以达到&#xf…...

Spring Boot 实现流式响应(兼容 2.7.x)

在实际开发中&#xff0c;我们可能会遇到一些流式数据处理的场景&#xff0c;比如接收来自上游接口的 Server-Sent Events&#xff08;SSE&#xff09; 或 流式 JSON 内容&#xff0c;并将其原样中转给前端页面或客户端。这种情况下&#xff0c;传统的 RestTemplate 缓存机制会…...

2.Vue编写一个app

1.src中重要的组成 1.1main.ts // 引入createApp用于创建应用 import { createApp } from "vue"; // 引用App根组件 import App from ./App.vue;createApp(App).mount(#app)1.2 App.vue 其中要写三种标签 <template> <!--html--> </template>…...

(二)原型模式

原型的功能是将一个已经存在的对象作为源目标,其余对象都是通过这个源目标创建。发挥复制的作用就是原型模式的核心思想。 一、源型模式的定义 原型模式是指第二次创建对象可以通过复制已经存在的原型对象来实现,忽略对象创建过程中的其它细节。 📌 核心特点: 避免重复初…...

智能分布式爬虫的数据处理流水线优化:基于深度强化学习的数据质量控制

在数字化浪潮席卷全球的今天&#xff0c;数据已成为企业和研究机构的核心资产。智能分布式爬虫作为高效的数据采集工具&#xff0c;在大规模数据获取中发挥着关键作用。然而&#xff0c;传统的数据处理流水线在面对复杂多变的网络环境和海量异构数据时&#xff0c;常出现数据质…...

Web 架构之 CDN 加速原理与落地实践

文章目录 一、思维导图二、正文内容&#xff08;一&#xff09;CDN 基础概念1. 定义2. 组成部分 &#xff08;二&#xff09;CDN 加速原理1. 请求路由2. 内容缓存3. 内容更新 &#xff08;三&#xff09;CDN 落地实践1. 选择 CDN 服务商2. 配置 CDN3. 集成到 Web 架构 &#xf…...

R语言速释制剂QBD解决方案之三

本文是《Quality by Design for ANDAs: An Example for Immediate-Release Dosage Forms》第一个处方的R语言解决方案。 第一个处方研究评估原料药粒径分布、MCC/Lactose比例、崩解剂用量对制剂CQAs的影响。 第二处方研究用于理解颗粒外加硬脂酸镁和滑石粉对片剂质量和可生产…...

GruntJS-前端自动化任务运行器从入门到实战

Grunt 完全指南&#xff1a;从入门到实战 一、Grunt 是什么&#xff1f; Grunt是一个基于 Node.js 的前端自动化任务运行器&#xff0c;主要用于自动化执行项目开发中重复性高的任务&#xff0c;例如文件压缩、代码编译、语法检查、单元测试、文件合并等。通过配置简洁的任务…...

人工智能--安全大模型训练计划:基于Fine-tuning + LLM Agent

安全大模型训练计划&#xff1a;基于Fine-tuning LLM Agent 1. 构建高质量安全数据集 目标&#xff1a;为安全大模型创建高质量、去偏、符合伦理的训练数据集&#xff0c;涵盖安全相关任务&#xff08;如有害内容检测、隐私保护、道德推理等&#xff09;。 1.1 数据收集 描…...

如何在Windows本机安装Python并确保与Python.NET兼容

✅作者简介&#xff1a;2022年博客新星 第八。热爱国学的Java后端开发者&#xff0c;修心和技术同步精进。 &#x1f34e;个人主页&#xff1a;Java Fans的博客 &#x1f34a;个人信条&#xff1a;不迁怒&#xff0c;不贰过。小知识&#xff0c;大智慧。 &#x1f49e;当前专栏…...