SpringBoot使用esayExcel根据模板导出excel

1、依赖

       <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.1.3</version></dependency>

2、模板

3、实体类

package com.skybird.iot.addons.productionManagement.qualityTesting.backend.jdo;import com.alibaba.excel.annotation.ExcelProperty;public class qualityTestingExcelDao {@ExcelProperty("序号")private String index;@ExcelProperty("质检类别")private String workingProcedure;@ExcelProperty("检查部位")private String inspectionPart;@ExcelProperty("检查内容")private String inspectionContent;@ExcelProperty("检查方法")private String inspectionMethod;@ExcelProperty("检查结果")private String inspectionResult;@ExcelProperty("备注")private String notes;public String getIndex() {return index;}public void setIndex(String index) {this.index = index;}public String getWorkingProcedure() {return workingProcedure;}public void setWorkingProcedure(String workingProcedure) {this.workingProcedure = workingProcedure;}public String getInspectionPart() {return inspectionPart;}public void setInspectionPart(String inspectionPart) {this.inspectionPart = inspectionPart;}public String getInspectionContent() {return inspectionContent;}public void setInspectionContent(String inspectionContent) {this.inspectionContent = inspectionContent;}public String getInspectionMethod() {return inspectionMethod;}public void setInspectionMethod(String inspectionMethod) {this.inspectionMethod = inspectionMethod;}public String getInspectionResult() {return inspectionResult;}public void setInspectionResult(String inspectionResult) {this.inspectionResult = inspectionResult;}public String getNotes() {return notes;}public void setNotes(String notes) {this.notes = notes;}@Overridepublic String toString() {return "qualityTestingExcelDao{"+ "index='"+ index+ '\''+ ", workingProcedure='"+ workingProcedure+ '\''+ ", inspectionPart='"+ inspectionPart+ '\''+ ", inspectionContent='"+ inspectionContent+ '\''+ ", inspectionMethod='"+ inspectionMethod+ '\''+ ", inspectionResult='"+ inspectionResult+ '\''+ ", notes='"+ notes+ '\''+ '}';}
}

4、接口

private static List<qualityTestingExcelDao> getList(Document dto) {List<Document> list = DocuLib.getList(dto, "qualityInspectionList");List<qualityTestingExcelDao> excelList = new ArrayList<>();// 用于记录当前质检序号int index = 0;// 用于记录当前质检类别String inspectionPart = "";for (int i = 0; i < list.size(); i++) {Document item = list.get(i);String workingProcedure = DocuLib.getStr(item, "workingProcedure");List<Document> detectionList = DocuLib.getList(item, "detectionList");if (ObjectUtils.isNotEmpty(detectionList)) {for (Document row : detectionList) {qualityTestingExcelDao dao = new qualityTestingExcelDao();String inspectionPartDb = DocuLib.getStr(row, "project");if (!inspectionPart.equals(inspectionPartDb)) {inspectionPart = inspectionPartDb;index++;}dao.setIndex(String.valueOf(index));dao.setWorkingProcedure(workingProcedure);dao.setInspectionPart(inspectionPartDb);dao.setInspectionContent(DocuLib.getStr(row, "content"));dao.setInspectionMethod(DocuLib.getStr(row, "inspectionMethods.name"));dao.setInspectionResult(DocuLib.getStr(row, "result.name"));dao.setNotes(DocuLib.getStr(row, "illustrate"));excelList.add(dao);}} else {qualityTestingExcelDao dao = new qualityTestingExcelDao();dao.setIndex(String.valueOf(index));dao.setWorkingProcedure(workingProcedure);excelList.add(dao);}}return excelList;}/*** 根据模板下载** @param response* @throws IOException*/@GetMapping("/excel")public void excel(HttpServletResponse response, @RequestParam("id") String id)throws IOException {try {Document dto = DBUtils.find(qualityTesting.collectionName, new Document("id", id));List<qualityTestingExcelDao> excelList = getList(dto);InputStream templateStream =qualityTestingWeb.class.getResourceAsStream("/templates/qualityTesting.xlsx");if (templateStream == null) {throw new FileNotFoundException("未找到模板文件");}// 生成目标文件ExcelWriter excelWriter =EasyExcel.write(response.getOutputStream()).withTemplate(templateStream).build();WriteSheet writeSheet = EasyExcel.writerSheet().build();// 每次都会重新生成新的一行,而不是使用下面的空行FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();// 替换第一种占位符Map<String, Object> map = new HashMap<>();map.put("name", DocuLib.getStr(dto, "productName"));map.put("userName", DocuLib.getStr(dto, "completeBy.name"));map.put("time", DocuLib.getStr(dto, "completeDate"));excelWriter.fill(map, writeSheet);// 第二种占位符替换,这里定义了 hisDataexcelWriter.fill(new FillWrapper("dto", excelList), fillConfig, writeSheet);excelWriter.finish();// 设置响应头response.setContentType("application/vnd.ms-excel"); // 设置文本内省response.setCharacterEncoding("utf-8"); // 设置字符编码response.setHeader("Content-disposition", "attachment;fileName=name.xlsx");// 关闭模板流templateStream.close();} catch (FileNotFoundException e) {// 处理文件未找到异常response.setStatus(HttpServletResponse.SC_NOT_FOUND);// 返回适当的错误消息response.getWriter().write("未找到模板文件");} catch (Exception e) {// 处理其他异常response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);// 返回适当的错误消息response.getWriter().write("内部服务器错误");}}

5、效果

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

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

泰始明昌文旅:如何打造真正的文旅爆品体系

泰始明昌文旅&#xff1a;如何打造真正的文旅爆品体系 泰始明昌文旅&#xff1a;如何打造真正的爆品体系 关键词&#xff1a;泰始明昌文旅,文旅爆品,核心卖点,用户痛点,项目特点,对手弱点,爆品体系,爆品品类,结构化,品质,价值链接,生态体系,营销推广,持续创新 摘要&#xff…

接口和多态

接口 概念 接口是功能的集合&#xff0c;它同样是一种引用数据类型&#xff0c;可以把接口看作抽象类更为抽象的 "类"。 接口只描述所应该具备的功能方法&#xff0c;但是没有具体的方法实现&#xff0c;即接口中具有的都是抽象方法&#xff0c;这些抽象方法的实现是…

美发店业务流程优化:SpringBoot管理系统

2相关技术 2.1 MYSQL数据库 MySQL是一个真正的多用户、多线程SQL数据库服务器。 是基于SQL的客户/服务器模式的关系数据库管理系统&#xff0c;它的有点有有功能强大、使用简单、管理方便、安全可靠性高、运行速度快、多线程、跨平台性、完全网络化、稳定性等&#xff0c;非常…

Java | Leetcode Java题解之第468题验证IP地址

题目&#xff1a; 题解&#xff1a; class Solution {public String validIPAddress(String queryIP) {if (queryIP.indexOf(.) > 0) {// IPv4int last -1;for (int i 0; i < 4; i) {int cur (i 3 ? queryIP.length() : queryIP.indexOf(., last 1));if (cur <…

汽车3d动效的作用!云渲染实现3d动效

在汽车营销领域&#xff0c;3D动效技术以其独特的视觉冲击力和交互体验&#xff0c;正成为吸引消费者注意力的新利器。而云渲染技术的应用&#xff0c;更是让这些动效如虎添翼&#xff0c;实现了高效、低成本的3D视觉内容制作与分享。本文将探讨汽车3D动效的作用&#xff0c;并…

java中的I/O(8个案例+代码+效果图)

目录 1.File类 1&#xff09;常用构造方法 1&#xff09;File(String pathname) 2&#xff09;File(String parent, String child) 3&#xff09;File(File parent, String child) 2&#xff09;常用方法 1&#xff09;boolean canRead() 2&#xff09;boolean canWrite() 3&am…

SiLM266x系列SiLM2661高压电池组前端充/放电高边NFET驱动器 为电池系统保护提供可靠性和设计灵活性

SiLM2661产品概述&#xff1a; SiLM2661能够灵活的应对不同应用场景对锂电池进行监控和保护的需求&#xff0c;为电池系统保护提供可靠性和设计灵活性。是用于电池充电/放电系统控制的低功耗、高边 N 沟道 FET 驱动器&#xff0c;高边保护功能可避免系统的接地引脚断开连接&am…

Linux云计算 |【第四阶段】RDBMS2-DAY2

主要内容&#xff1a; 数据读写分离概述、Maxscale实现数据读写分离、多实例配置、中间件概述 一、数据读写分离概述 数据读写分离&#xff08;Read/Write Splitting&#xff09;是一种数据库架构设计模式&#xff0c;旨在提高数据库系统的性能、可扩展性和可用性。通过将读操…

how to increase the height of the ps or cdm window

when the line reaches the bottom; directly pull up the top bar of the window after pulling down the bar

Llama-3.2-3B-Instruct PyTorch模型微调最佳实践

1 引言 Meta Llama 3.2多语言大型语言模型集合&#xff08;LM&#xff09;是一个1B和3B大小&#xff08;文本输入/文本输出&#xff09;的预训练和指令微调模型集合。Llama 3.2指令调整的纯文本模型针对多语言对话用例进行了优化&#xff0c;包括智能检索和总结任务。它们在常…

Vue组件继承与扩展

Vue组件继承与扩展 前言 与Class继承类似&#xff0c;在Vue中可以通过组件继承来达到复用和扩展基础组件的目的&#xff0c;虽然它可能会带来一些额外的性能损耗和维护成本&#xff0c;但其在解决一些非常规问题时有奇效。本文将通过一些非常规的功能需求来讨论其实现过程。 …

锐明Crocus系统 RepairRecord.do SQL注入漏洞

0x01 产品描述&#xff1a; 明锐技术是一家专注于AI和视频技术的商用车智能物联&#xff08;AIoT&#xff09;解决方案提供商&#xff0c;Crocus系统是其核心产品之一。该系统旨在利用人工智能、高清视频、大数据和自动驾驶技术&#xff0c;提高企业或车队的运营效率&#xff0…

python31_format方法的使用

format方法的使用 def str_format(c, d):"""格式化字符串函数本函数的目的是通过给定的参数c和d&#xff0c;去填充一个预设的字符串模板。模板中包含了{name1}和{name2}两个占位符&#xff0c;分别用参数c和d的值来替换。这个过程演示了Python中字符串格式化的…

AR虚拟试用,让网购不再只靠想象!

在数字化浪潮席卷全球的今天&#xff0c;电子商务已成为我们日常生活中不可或缺的一部分。然而&#xff0c;传统的网购体验往往受限于二维图片和文字描述&#xff0c;消费者在购买前只能依靠想象来构建商品的模样与适用性&#xff0c;这无疑增加了购物的不确定性和风险。幸运的…

基于卷积神经网络的脊柱骨折识别系统,resnet50,mobilenet模型【pytorch框架+python】

更多目标检测和图像分类识别项目可看我主页其他文章 功能演示&#xff1a; 基于卷积神经网络的脊柱骨折识别系统&#xff0c;resnet50&#xff0c;mobilenet【pytorch框架&#xff0c;python&#xff0c;tkinter】_哔哩哔哩_bilibili &#xff08;一&#xff09;简介 基于卷…

Java面试宝典-Java集合02

目录 Java面试宝典-Java集合02 21、TreeMap 和 TreeSet 在排序时如何比较元素&#xff1f; 22、ArrayList 和 LinkedList 的区别是什么&#xff1f; 23、ArrayList 和 Vector 的区别&#xff1f; 24、队列和栈是什么&#xff1f;有什么区别&#xff1f; 25、Queue和Deque的区别…

版本控制系统Helix Core的常见使用误区及解决办法、实用工具及新功能介绍

日前&#xff0c;Perforce携手合作伙伴龙智一同亮相Unreal Fest 2024上海站&#xff0c;分享Helix Core版本控制系统及其协作套件的强大功能与最新动态&#xff0c;助力游戏创意产业加速前行。 Perforce解决方案工程师Kory Luo在活动主会场&#xff0c;带来《Perforce Helix C…

谈谈英国硕士毕业论文如何收集问卷数据

在之前几期文章中&#xff0c;英国翰思教育小编介绍了在写英国硕士毕业论文的时候如何设计问卷&#xff0c;以及设计问卷时需要注意的一些原则和细节。希望通过那些往期内容&#xff0c;可以帮助同学们正确认识毕业论文问卷设计&#xff0c;也可以帮助大家找到合适的问卷设计思…

vantUI用popup和picker和field做关键词搜索功能

<van-field class"mainFileCss" v-model"currentflowmeterLocation" left-icon"search" click"openPicker" center label"位号"placeholder"请输入位号"><template slot"button"></temp…

Linux查看下nginx及使用的配置文件

1、查到nginx进程 ps -aef | grep nginx2、通过进行pid查到nginx路径 pwdx <pid>3、根据路径得到配置文件 path***/nginx -t如下&#xff1a;