SpringBoot使用esayExcel根据模板导出excel

news2024/10/10 16:59:12

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;
  }

  @Override
  public 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);
      // 第二种占位符替换,这里定义了 hisData
      excelWriter.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.coloradmin.cn/o/2202556.html

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

相关文章

流程图 LogicFlow

流程图 LogicFlow 官方文档&#xff1a;https://site.logic-flow.cn/tutorial/get-started <script setup> import { onMounted, ref } from vue import { forEach, map, has } from lodash-es import LogicFlow, { ElementState, LogicFlowUtil } from logicflow/core …

字符编码发展史6 — BOM字节序标记

上一篇《字符编码发展史5 — UTF-16和UTF-32》我们讲解了UTF-16和UTF-32编码。本篇我们将继续讲解字符编码中的字节序标记(BOM)。 2.3. 第三个阶段 国际化 2.3.2. Unicode的编码方式 2.3.2.5. BOM 1. 什么是BOM&#xff1f; BOM是Byte Order Mark的缩写&#xff0c;翻译成…

研究生异地报名,需要社保缴费记录,没有社保记录怎么办。

1、户籍在安徽省&#xff0c;在北京工作&#xff0c;想报北京科技大学&#xff1b; 招生简章中没有提社保记录&#xff0c;但是在报名的时候&#xff0c;又出来要求&#xff1a;北京连续6个月的社保记录。这里是指在北京市考试的要求。没有连续社保缴费记录&#xff0c;肯定不能…

Python 与 Pycharm 的简易安装教程,包含Pycharm的修改

一. 官方网站 Python网址&#xff1a;python唯一的官方网址。 Pycharm网址&#xff1a;Pycharm的官方网址。 二. python安装步骤 滑动到红色框内 Downloads 导航栏。 红色框是选择适合自己电脑系统和版本的部分&#xff0c;蓝色框是选择系统的部分&#xff0c;黄色框是版本号。…

【大数据】数据分析之Spark框架介绍

文章目录 概述一、发展历程与背景二、核心特点三、生态系统与组件四、应用场景五、与其他大数据技术的比较 核心概念1. 弹性分布式数据集&#xff08;RDD, Resilient Distributed Dataset&#xff09;2. 转换&#xff08;Transformations&#xff09;和动作&#xff08;Actions…

Rust编程的函数

【图书介绍】《Rust编程与项目实战》-CSDN博客 《Rust编程与项目实战》(朱文伟&#xff0c;李建英)【摘要 书评 试读】- 京东图书 (jd.com) Rust编程与项目实战_夏天又到了的博客-CSDN博客 7.1 函 数 定 义 在Rust中&#xff0c;函数使用fn关键字定义&#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

【Linux】ComfyUI和SD WebUI之PYTHON环境共享,模型共享,LORA等公共资源共享

需求 一般玩AI绘图都会装ComfyUI和SD WebUI。而且这俩的模型、lora等都是一致的。为了避免空间的浪费&#xff0c;一般会采用共享数据的方式。而且共享的数据可以任意指定分区&#xff0c;这让挂载NAS共享空间成为可能&#xff0c;实现多绘画机ComfyUI和SD WebUI共享资源。 实…

攀爬数据集,约500张 !VOC格式,yolo可直接使用~真实场景特征明显高清图,yolo可直接使用!

攀爬数据集&#xff0c;约500张&#xff01;&#xff01;&#xff01; VOC格式&#xff0c;yolo可直接使用&#xff5e; 真实场景特征明显高清图&#xff0c;yolo可直接使用&#xff01; 攀爬数据集&#xff0c;约500张&#xff01;&#xff01;&#xff01; VOC格式&#xff0…

用GPT-4o打造LLM+OS(10+实用技能),代码开源,指令曝光,科技演示惊艳全场!

目录 前言 LLM操作系统能力概况&#xff08;phidata中前5个已经实现&#xff09;&#xff1a; 可以读取/生成文本 拥有比任何单个人类更全面的知识 可以浏览互联网 可以使用现有的软件基础设施&#xff08;计算器、Python、鼠标/键盘&#xff09; 可以与其他LLMs通信 可…

一文了解,ARM 工业计算机的发展历程

ARM 工业计算机的发展历程主要经历了以下几个阶段&#xff1a; 早期探索阶段&#xff08;20 世纪 80 年代 - 90 年代初&#xff09;&#xff1a; 起源背景&#xff1a;20 世纪 80 年代&#xff0c;计算机工业蓬勃发展&#xff0c;英国的 Acorn 公司在这一时期积极探索芯片技术…

Unity实现自定义图集(四)

以下内容是根据Unity 2020.1.0f1版本进行编写的   在之前的篇章中已经把自定义图集在编辑器上的使用,以及运行时所需的信息都准备好了,接下来就是魔改UGUI的Image组件,使其能够像Image那样运行时如果引用的资源有打自定义图集,则加载对应自定义图集的Texture。 1、思路 …

Centos7通过jengkins实现自动发布和回滚

一、安装jenkins 注&#xff1a;这里不多说哈&#xff0c;百度遍地都是&#xff0c;安装方式不限。 二、jenkins创建项目 注&#xff1a;这里有个坑需要说一下&#xff0c;最开始我使用的是maven构建&#xff0c;但是如果按照我的这套方案会有一个编译死循环的问题&#xff0c;…

【Linux】多进程服务器模型(第十九篇)

目录 一、定义与工作原理 二、特点与优势 三、实现与示例 四、注意事项 多进程服务器模型是一种在服务器端使用的并发处理模型&#xff0c;它允许服务器同时处理多个客户端的请求。以下是关于多进程服务器模型的详细介绍&#xff1a; 一、定义与工作原理 定义&#xff1a;…

抽象类Abstart Class

抽象类其实就是一种不完全的设计图 必须用abstract修饰 模板方法&#xff1a;建议使用final修饰&#xff0c;不能被重写。

提高ROI:低代码平台如何助力企业实现成本效益最大化

引言&#xff1a;成本效益与ROI的重要性 在当今竞争异常激烈的商业环境中&#xff0c;企业面临着前所未有的挑战。如何在有限的资源下&#xff0c;最大化投资回报率&#xff08;ROI&#xff09;&#xff0c;已经成为企业管理者不可忽视的关键课题。ROI不仅仅是衡量投资回报的指…

PROFINET 转 EtherCAT, EtherCAT/Ethernet/IP/Profinet/ModbusTCP协议互转工业串口网关

EtherCAT/Ethernet/IP/Profinet/ModbusTCP协议互转工业串口网关https://item.taobao.com/item.htm?ftt&id822721028899 协议转换通信网关 PROFINET 转 EtherCAT GW系列型号 MS-GW31 概述 简介 MS-GW31 是 PROFINET 和 EtherCAT 协议转换网关&#xff0c;为用户提供两…

服装生产管理的数字化转型:SpringBoot框架

4 系统设计 4.1 系统结构设计 在结构设计过程中&#xff0c;首先对系统进行需求分析&#xff0c;然后进行系统初步设计&#xff0c;将系统功能模块细化&#xff0c;具体分析每一个功能模块具体应该首先哪些功能&#xff0c;最后将各个模块进行整合&#xff0c;实现系统结构的…

Javascript动态规划算法

JavaScript中的动态规划&#xff08;Dynamic Programming&#xff0c;简称DP&#xff09;是一种通过把原问题分解为相对简单的子问题的方式来求解复杂问题的方法。它主要致力于将“合适”的问题拆分成更小的子目标&#xff0c;并通过建立状态转移方程、缓存并复用以往结果以及按…

【完-网络安全】Shell与脚本

文章目录 1.CLI与GUI2.终端和Shell2.1 Shell 壳层2.2 终端2.3 终端和Shell区别3.标准流 4.PowerShell4.1 管理员与非管理员4.2 指令4.3 重定向4.4 管道 5.环境变量5.1 影响范围5.2环境变量的作用5.3 常见的环境变量 6.脚本 1.CLI与GUI CLI命令行界面(CLl,Command Line Interfa…