生成带分表和水印的excel压缩文件

功能描述

将查询结果生成带分表和水印的excel压缩文件


功能点

1、将查询结果导出为excel文件

2、每个表格存放50万条数据,超过50万条数据,生成新的分表

3、生成的表格需要添加水印

4、将生成的全部分表,打包成zip压缩文件


引入依赖

<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.3.2</version><exclusions><exclusion><artifactId>commons-compress</artifactId><groupId>org.apache.commons</groupId></exclusion></exclusions></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>ooxml-schemas</artifactId><version>1.4</version></dependency><!-- zip处理工具包 --><dependency><groupId>net.lingala.zip4j</groupId><artifactId>zip4j</artifactId><version>1.3.2</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.19</version><scope>compile</scope></dependency>
package com.cusc.product.common.util.excel;import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.cusc.product.common.util.WaterMarkCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;import java.io.File;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.function.Supplier;/*** Excel文件生成** @author liubin*/
@Slf4j
public class EasyExcelWriteCommonUtil {/*** 文件行数限制*/private static final int EXCEL_LIMIT_NUM = 500000;/*** 数据写入Excel文件,多文件 自定义表头** @param suppliers 自定义函数* @param listFiled 表头* @param dirName   期待的压缩文件的名称* @return zip压缩后的文件*/public static File createMultiFileExcelZipFiles(List<Supplier<List>> suppliers, List<String> listFiled,Class clzss, String dirName, String waterMarkUserName, String waterMarkUsePhone) {File zipFile;// 创建临时目录if (Objects.isNull(dirName)) {dirName = UUID.randomUUID().toString();}File localDir = createDir(FileBaseCommonUtil.TEMPORARY_DIRECTORY_PATH + "/" + dirName);log.info("临时目录: " + localDir.getPath());String sheet = "Sheet1";// 文件批次编号int fileId = 1;// 单个文件累积写入数据行数int number = 0;ExcelWriter excelWriter = null;WriteSheet writeSheet = null;//只导出这几列Set<String> includeColumnFiledNames = new LinkedHashSet<>();for (int j = 0; j < listFiled.size(); j++) {includeColumnFiledNames.add(listFiled.get(j));}try {for (int i = 0; i < suppliers.size(); i++) {List dataList = suppliers.get(i).get();if (dataList.size() > EXCEL_LIMIT_NUM) {log.error("Supplier返回数据量最大不可超过{}", EXCEL_LIMIT_NUM);throw new RuntimeException(String.format("Supplier返回数据量最大不可超过%s", EXCEL_LIMIT_NUM));}number += dataList.size();if (number > EXCEL_LIMIT_NUM) {// 数据量累积超出了, 需要新建文件记录int comNum = number - EXCEL_LIMIT_NUM;List compensate = dataList.subList(dataList.size() - comNum, dataList.size());List oldDataList = dataList.subList(0, dataList.size() - comNum);// 旧文件追加数据File oldFile = new File(localDir, dirName + "_" + fileId + ".xlsx");if (oldFile.exists()) {excelWriter.write(oldDataList, writeSheet);// 添加水印Workbook workbook = excelWriter.writeContext().writeWorkbookHolder().getWorkbook();WaterMarkCommonUtil.insertWaterMarkNamePhoneToXlsx(workbook, waterMarkUserName, waterMarkUsePhone);excelWriter.finish();}if (CollectionUtils.isNotEmpty(compensate)) {// 写入新的文件数据File file = new File(localDir, dirName + "_" + (++fileId) + ".xlsx");excelWriter = EasyExcelFactory.write(file.getAbsolutePath()).build();writeSheet = EasyExcel.writerSheet(i, sheet).head(clzss).includeColumnFieldNames(includeColumnFiledNames).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
//                        writeSheet = EasyExcelFactory.writerSheet(sheet).build();excelWriter.write(dataList, writeSheet);// 添加水印Workbook workbook = excelWriter.writeContext().writeWorkbookHolder().getWorkbook();WaterMarkCommonUtil.insertWaterMarkNamePhoneToXlsx(workbook, waterMarkUserName, waterMarkUsePhone);// 重置记录数number = compensate.size();}if (CollectionUtils.isNotEmpty(oldDataList)) {oldDataList.clear();}if (CollectionUtils.isNotEmpty(compensate)) {compensate.clear();}if (CollectionUtils.isNotEmpty(dataList)) {dataList.clear();}continue;}File file = new File(localDir, dirName + "_" + fileId + ".xlsx");if (file.exists()) {excelWriter.write(dataList, writeSheet);// 添加水印Workbook workbook = excelWriter.writeContext().writeWorkbookHolder().getWorkbook();WaterMarkCommonUtil.insertWaterMarkNamePhoneToXlsx(workbook, waterMarkUserName, waterMarkUsePhone);} else {excelWriter = EasyExcelFactory.write(file.getAbsolutePath()).build();writeSheet = EasyExcel.writerSheet(i, sheet).head(clzss).includeColumnFieldNames(includeColumnFiledNames).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();excelWriter.write(dataList, writeSheet);// 添加水印Workbook workbook = excelWriter.writeContext().writeWorkbookHolder().getWorkbook();WaterMarkCommonUtil.insertWaterMarkNamePhoneToXlsx(workbook, waterMarkUserName, waterMarkUsePhone);}if (CollectionUtils.isNotEmpty(dataList)) {dataList.clear();}}} catch (RuntimeException e) {log.error("数据导出失败", e);throw new RuntimeException(e);} catch (IOException e) {log.error("数据导出失败", e);throw new RuntimeException(e);} finally {if (excelWriter != null) {excelWriter.finish();}}String zipPath = Zip4jCommonUtil.zip(localDir.getPath());deleteFile(localDir);if (StringUtils.isEmpty(zipPath)) {log.error("文件压缩失败");throw new RuntimeException("文件压缩失败");}zipFile = new File(zipPath);log.info("临时目录压缩后路径: " + zipPath);return zipFile;}/*** 删除文件目录** @param file* @return*/private static Boolean deleteFile(File file) {// 判断文件不为null或文件目录存在if (file == null || !file.exists()) {log.error("文件删除失败,请检查文件是否存在以及文件路径是否正确");return false;}// 获取目录下子文件File[] files = file.listFiles();// 遍历该目录下的文件对象for (File f : files) {// 判断子目录是否存在子目录,如果是文件则删除if (f.isDirectory()) {// 递归删除目录下的文件deleteFile(f);} else {// 文件删除f.delete();}}// 文件夹删除file.delete();return true;}/*** 创建目录** @param localDirPath 目录路径* @return File对象*/private static File createDir(String localDirPath) {File localDir = new File(localDirPath);if (!localDir.exists()) {localDir.mkdirs();}return localDir;}}
package com.cusc.product.common.util;import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReflectUtil;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFPictureData;
import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;public class WaterMarkCommonUtil {/*** Excel 导出添加水印** @param workbook ExcelWorkbook* @param userName 导出人姓名*/public static void insertWaterMarkNamePhoneToXlsx(Workbook workbook, String userName, String phone) throws IOException {String date = DateUtil.now();String waterMarkText = userName + "\n" + phone;if (workbook instanceof SXSSFWorkbook) {insertWaterMarkTextToXlsx((SXSSFWorkbook) workbook, waterMarkText);} else if (workbook instanceof XSSFWorkbook) {insertWaterMarkTextToXlsx((XSSFWorkbook) workbook, waterMarkText);}//throw new RemoteException("HSSFWorkbook 模式不支持 Excel 水印");}/*** Excel 导出添加水印** @param workbook ExcelWorkbook* @param userName 导出人姓名*/public static void insertWaterMarkTextToXlsx(Workbook workbook, String userName) throws IOException {String date = DateUtil.now();String waterMarkText = userName + "\n" + date;if (workbook instanceof SXSSFWorkbook) {insertWaterMarkTextToXlsx((SXSSFWorkbook) workbook, waterMarkText);} else if (workbook instanceof XSSFWorkbook) {insertWaterMarkTextToXlsx((XSSFWorkbook) workbook, waterMarkText);}//throw new RemoteException("HSSFWorkbook 模式不支持 Excel 水印");}/*** 给 Excel 添加水印** @param workbook      SXSSFWorkbook* @param waterMarkText 水印文字内容*/private static void insertWaterMarkTextToXlsx(SXSSFWorkbook workbook, String waterMarkText) throws IOException {BufferedImage image = createWatermarkImage(waterMarkText);ByteArrayOutputStream imageOs = new ByteArrayOutputStream();ImageIO.write(image, "png", imageOs);int pictureIdx = workbook.addPicture(imageOs.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG);XSSFPictureData pictureData = (XSSFPictureData) workbook.getAllPictures().get(pictureIdx);for (int i = 0; i < workbook.getNumberOfSheets(); i++) {//获取每个Sheet表SXSSFSheet sheet = workbook.getSheetAt(i);//这里由于 SXSSFSheet 没有 getCTWorksheet() 方法,通过反射取出 _sh 属性XSSFSheet shReflect = (XSSFSheet) ReflectUtil.getFieldValue(sheet, "_sh");PackagePartName ppn = pictureData.getPackagePart().getPartName();String relType = XSSFRelation.IMAGES.getRelation();PackageRelationship pr = shReflect.getPackagePart().addRelationship(ppn, TargetMode.INTERNAL, relType, null);shReflect.getCTWorksheet().addNewPicture().setId(pr.getId());}imageOs.close();}/*** 给 Excel 添加水印** @param workbook      XSSFWorkbook* @param waterMarkText 水印文字内容*/private static void insertWaterMarkTextToXlsx(XSSFWorkbook workbook, String waterMarkText) throws IOException {BufferedImage image = createWatermarkImage(waterMarkText);ByteArrayOutputStream imageOs = new ByteArrayOutputStream();ImageIO.write(image, "png", imageOs);int pictureIdx = workbook.addPicture(imageOs.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG);XSSFPictureData pictureData = workbook.getAllPictures().get(pictureIdx);for (int i = 0; i < workbook.getNumberOfSheets(); i++) {//获取每个Sheet表XSSFSheet sheet = workbook.getSheetAt(i);PackagePartName ppn = pictureData.getPackagePart().getPartName();String relType = XSSFRelation.IMAGES.getRelation();PackageRelationship pr = sheet.getPackagePart().addRelationship(ppn, TargetMode.INTERNAL, relType, null);sheet.getCTWorksheet().addNewPicture().setId(pr.getId());}imageOs.close();}/*** 创建水印图片** @param waterMark 水印文字*/private static BufferedImage createWatermarkImage(String waterMark) {String[] textArray = waterMark.split("\n");Font font = new Font("microsoft-yahei", Font.PLAIN, 24);
//        int width = 500;
//        int height = 400;int width = 300;int height = 200;BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 背景透明 开始Graphics2D g = image.createGraphics();image = g.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);g.dispose();// 背景透明 结束g = image.createGraphics();g.setColor(new Color(237, 235, 233));// 设定画笔颜色g.setFont(font);// 设置画笔字体//   g.shear(0.1, -0.26);// 设定倾斜度//        设置字体平滑g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//文字从中心开始输入,算出文字宽度,左移动一半的宽度,即居中FontMetrics fontMetrics = g.getFontMetrics(font);// 水印位置int x = width / 2;int y = height / 2;// 设置水印旋转g.rotate(Math.toRadians(-40), x, y);for (String s : textArray) {// 文字宽度int textWidth = fontMetrics.stringWidth(s);g.drawString(s, x - (textWidth / 2), y);// 画出字符串y = y + font.getSize();}g.dispose();// 释放画笔return image;}}

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

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

相关文章

IS200EPSMG1AED 使用Lua创建逻辑脚本或完整程序

IS200EPSMG1AED 使用Lua创建逻辑脚本或完整程序 IS200EPSMG1AED 是一种支持网络的I/O控制器&#xff0c;它执行类似于可编程逻辑控制器(PLC)的控制、逻辑和监控功能。然而&#xff0c;与PLC不同&#xff0c;X-600M是为基于网络的应用而设计的。X-600M可以使用内置网络服务器和…

2010年408计网

下列选项中, 不属于网络体系结构所描述的内容是&#xff08;C&#xff09;A. 网络的层次B. 每层使用的协议C. 协议的内部实现细节D. 每层必须完成的功能 本题考查网络体系结构的相关概念 再来看当今世界最大的互联网&#xff0c;也就是因特网。它所采用的TCP/IP 4层网络体系结…

淘宝婴儿用品购买情况分析报告

一.分析背景和目的 随着购物网站的发展&#xff0c;人们的网络购物行为占比也快速增加。为了能够获取更多的用户&#xff0c;提升商家的销售量&#xff0c;需要从产品和用户不同的角度进行分析&#xff0c;进而得到有价值的信息&#xff0c;指导商家进行获客和营销。本文就以淘…

11月起,33个省份纳入数电票开票试点范围内,发票无纸化已是大势所趋!

10月底&#xff0c;北京、贵州、山东&#xff08;不含青岛市&#xff09;、湖南、宁夏5个地区相继发布开展数电票试点工作的通知&#xff0c;至此&#xff0c;全国已有33个省份纳入数电票开票试点范围内。根据上述5地区发布的相关公告&#xff0c;11月1日将正式推行“数电票”开…

Java_类和对象详解

文章目录 前言简单认识类类定义和使用类的实例化引用的一些注意事项 类和对象的说明及关系this引用为什么要有this引用this应用this特性 构造方法构造特性及应用用this简化用idea编译器快捷创建构造 封装封装的概念访问限定符 封装的扩展-包包的概念导入包中的类自定义包常见的…

都用HTTPS了,还能被查出浏览记录?

最近&#xff0c;群里一个刚入职的小伙因为用公司电脑访问奇怪的网站&#xff0c;被约谈了。他很困惑 —— 访问的都是HTTPS的网站&#xff0c;公司咋知道他访问了啥&#xff1f; 实际上&#xff0c;由于网络通信有很多层&#xff0c;即使加密通信&#xff0c;仍有很多途径暴露…

Premiere Pro 2024 v24.0

adobe Premiere Pro 2024 Mac版发布了吗&#xff1f;无论您是编辑社交媒体视频还是大电影&#xff0c;Premiere Pro 都可以帮助您借助工具精心创作有意义的故事。导入和编辑&#xff0c;添加效果&#xff0c;然后将素材导出到任何目标。无论您要创作什么内容&#xff0c;它都可…

输电线路AR可视化巡检降低作业风险

随着现代工业的快速发展&#xff0c;各行业的一线技术工人要处理的问题越来越复杂&#xff0c;一些工作中棘手的问题迫切需要远端专家的协同处理。但远端专家赶来现场往往面临着专家差旅成本高、设备停机损失大、专业支持滞后、突发故障无法立即解决等痛点。传统的远程协助似乎…

CHS零壹视频恢复程序高级版视频修复OCR使用方法

目前CHS零壹视频恢复程序监控版、专业版、高级版已经支持了OCR&#xff0c;OCR是一种光学识别系统&#xff0c;高级版最新版本中不仅仅是在视频恢复中支持OCR&#xff0c;同时视频修复模块也增加了OCR功能&#xff0c;此功能可以针对一些批量修复的视频文件&#xff08;如执法仪…

【NI-DAQmx入门】NI-DAQmx之MATLAB/SIMULINK支持

Data Acquisition Toolbox™ 提供用于配置数据采集硬件、将数据读入 MATLAB 和 Simulink 以及将数据写入 DAQ 模拟和数字输出通道的应用程序和函数。该工具箱支持多种 DAQ 硬件&#xff0c;包括来自 National Instruments™ 和其他供应商的 USB、PCI、PCI Express 、PXI 和 PXI…

LangChain+LLM实战---ChatGPT的工作原理

一个词一个词的输出 ChatGPT能够自动生成类似于人类书写的文本&#xff0c;这是非常了不起和出乎意料的。但它是如何做到的&#xff1f;为什么会有效果呢&#xff1f;我的目的在于大致概述ChatGPT内部发生了什么&#xff0c;然后探讨它为什么能够很好地生成我们认为有意义的文…

HackTheBox-Starting Point--Tier 1---Funnel

文章目录 一 题目二 实验过程三 利用SSH隧道3.1 本地端口转发 一 题目 Tags FTP、PostgreSQL、Reconnaissance、Tunneling、Password Spraying、Port Forwarding、Anonymous/Guest Access、Clear Text Credentials译文&#xff1a;FTP、PostgreSQL、侦察、隧道技术、密码喷洒…