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

将数据赋值到多个文档里,并将多个word放入压缩包并下载

<!--        poi-tl导出word文档--><dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.12.1</version></dependency>

以下是需要的实体类

package com.dream.domain.vo;import lombok.Data;import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;@Data
public class QuestionVo  implements Serializable {private String quesTitle;private int type;private String numb;private ArrayList<AnwserVo> anwser;
}
package com.dream.domain.vo;import lombok.Data;import java.io.Serializable;@Data
public class AnwserVo  implements Serializable {private String content;private int type;private int supplementType;private String supplement;
}

以下是实现逻辑

package com.dream.controller.file;import com.deepoove.poi.XWPFTemplate;
import com.dream.common.BussinessException;
import com.dream.common.Result;
import com.dream.domain.vo.AnwserVo;
import com.dream.domain.vo.PageVo;
import com.dream.domain.vo.QuestionVo;
import com.dream.domain.vo.Student;
import com.dream.service.admin.QuestionnaireAdminService;
import com.dream.service.admin.UserMangeAdminService;
import com.dream.utils.Utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;@RestController
@RequestMapping("file")
public class FileUploadController {private final ResourceLoader resourceLoader;@Autowiredprivate QuestionnaireAdminService questionnaireAdminService;// 通过构造函数注入ResourceLoaderpublic FileUploadController(ResourceLoader resourceLoader) {this.resourceLoader = resourceLoader;}/*** 将数据赋值到多个文档里,并将多个word放入压缩包并下载* @param idsString* @param response*/@GetMapping("/exportBatch")public void exportBatchStudents(@RequestParam("idsString") String idsString, HttpServletResponse response) {List<Integer> ids = new ArrayList<>();ObjectMapper mapper = new ObjectMapper();try {if(idsString != null && !"".equals(idsString)){ids = mapper.readValue(idsString, new TypeReference<List<Integer>>() {});}} catch (JsonProcessingException e) {throw new BussinessException("导出问卷调查word列表压缩包,传入参数有误");}String name = Utils.dateToString("yyyy-MM-dd");try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {response.setContentType("application/zip");response.setCharacterEncoding("UTF-8");String zipName = URLEncoder.encode(name+" 问卷调查.zip", StandardCharsets.UTF_8.toString());response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + zipName);for (Integer id : ids) {try {// 1. 获取模板资源Resource resource = resourceLoader.getResource("classpath:templates/question.docx");if (!resource.exists()) throw new FileNotFoundException("模板文件未找到");// 2. 查询数据Map<String, ArrayList<QuestionVo>> result = questionnaireAdminService.queryUserQuestionnaireById(id);ArrayList<QuestionVo> list = result.get("subject");Map<String, String> data = new HashMap<>();// 3. 填充数据(完整迁移原有逻辑)if (list != null && !list.isEmpty()) {Map<String, Object> nameMap = questionnaireAdminService.queryNameByQuestionnaireId(id);String fileName = (nameMap != null && nameMap.get("name") != null) ?(String) nameMap.get("name") : "未知用户_" + id;// 核心数据填充逻辑(与单个导出完全一致)for (QuestionVo questionVo : list) {List<AnwserVo> anwser = questionVo.getAnwser();switch (questionVo.getNumb()) {case "one":for (int j = 0; j < anwser.size(); j++) {AnwserVo anwserVo = anwser.get(j);if (anwserVo.getType() == 1) {data.put("one_" + j, "√");}}break;case "two":for (int j = 0; j < anwser.size(); j++) {AnwserVo anwserVo = anwser.get(j);if (anwserVo.getType() == 1) {data.put("two_" + j, "√");}if (anwserVo.getSupplementType() == 1) {data.put("two_supplement", anwserVo.getSupplement());}}break;case "three":for (int j = 0; j < anwser.size(); j++) {AnwserVo anwserVo = anwser.get(j);if (anwserVo.getType() == 1) {data.put("three_" + j, "√");}}break;case "four":for (int j = 0; j < anwser.size(); j++) {AnwserVo anwserVo = anwser.get(j);if (anwserVo.getType() == 1) {data.put("four_" + j, "√");}if (anwserVo.getSupplementType() == 1) {data.put("four_supplement", anwserVo.getSupplement());}}break;case "five":for (int j = 0; j < anwser.size(); j++) {AnwserVo anwserVo = anwser.get(j);if (anwserVo.getType() == 1) {data.put("five_" + j, "√");}}break;case "six":for (int j = 0; j < anwser.size(); j++) {AnwserVo anwserVo = anwser.get(j);if (anwserVo.getType() == 1) {data.put("six_" + j, "√");}if (anwserVo.getSupplementType() == 1) {data.put("six_supplement", anwserVo.getSupplement());}}break;}}// 4. 生成文档并写入ZIPtry (XWPFTemplate template = XWPFTemplate.compile(resource.getInputStream()).render(data)) {String entryName = URLEncoder.encode(fileName + ".docx", "UTF-8").replaceAll("\\+", "%20");zos.putNextEntry(new ZipEntry(entryName));template.write(zos);zos.closeEntry();}}} catch (Exception e) {// 继续处理下一个问卷}}} catch (IOException e) {throw new RuntimeException("压缩包生成失败: " + e.getMessage(), e);}}}
    /*** 查询用户的问卷调查通过id   service层* @return*/@Overridepublic Map<String, ArrayList<QuestionVo>> queryUserQuestionnaireById(Integer id) {try {Map<String, Object> map1 = questionnaireAdminMapper.queryUserQuestionnaireById(id);if (map1 == null || map1.get("content") == null) {return Collections.singletonMap("subject", new ArrayList<>());}ObjectMapper objectMapper = new ObjectMapper();String subject = (String) map1.get("content");// 使用 TypeReference 指定泛型类型Map<String, ArrayList<QuestionVo>> map2 = objectMapper.readValue(subject,new TypeReference<Map<String, ArrayList<QuestionVo>>>() {});return map2;} catch (JsonProcessingException e) {throw new BussinessException("问卷调查转化异常");}}
    /*** 查询用户的问卷调查通过id  Mapper* @return*/@Select("select content from module_user_questionnaire where id = #{id}")Map queryUserQuestionnaireById(@Param("id") Integer id);

以下是导出模板

9、是否对中医药文化感兴趣?
A. 非常感兴趣  {{one_0}}
B.一般         {{one_1}}
C.不感兴趣     {{one_2}}10、想通过中医药元宇宙学习到什么知识?
A. 中医药基本理论  {{two_0}}
B.中医药临床知识   {{two_1}}
C.针灸推拿技术     {{two_2}}
D.食疗养生知识     {{two_3}}
E.其他,请填写!     {{two_4}}  {{two_supplement}}11、之前是否学习过中医药文化?
A.是  {{three_0}}
B.否  {{three_1}}12、每天入睡时间是_-点,睡眠时长为_-小时
A.21:00-23:00,7-8小时  {{four_0}}
B.23:00-1:00,6-7小时   {{four_1}}
C.1:00-3:00,5-6小时    {{four_2}}
E.其他,请填写:      {{four_3}}  {{four_supplement}}13、认为自己健康吗?
A. 非常健康  {{five_0}}
B.一般       {{five_1}}
C.不健康     {{five_2}}14、对健康的期许(可多选)
A. 身体健康,不生病  {{six_0}}
B.精神健康,心情愉快  {{six_1}}
C.饮食健康,胃口好、吃饭香  {{six_2}}
D.运动健康,体能充沛  {{six_3}}
E.其他,请填写:  {{six_4}}  {{six_supplement}}

http://www.dtcms.com/a/356827.html

相关文章:

  • Elasticsearch 9.X 使用推理 API 进行语义搜索
  • JAVA 请求第三方接口,将JSON数据转为对象
  • 微软正在公开测试其首个完全自主训练的大语言模型——MAI-1-preview
  • week5-[二维数组]翻转
  • 【性能优化】Unity 渲染优化全解析:Draw Call、Batch、SetPass 与批处理技术
  • 整理python接口自动化相关——10、自动考虑点(待续)
  • 动态规划--Day02--爬楼梯--2466. 统计构造好字符串的方案数,2533. 好二进制字符串的数量,2266. 统计打字方案数
  • 匠心传承,古韵新生——记木雕名家龙巍的艺术人生
  • v-model与v-bind区别
  • 吴恩达机器学习作业五:神经网络正向传播
  • Python Imaging Library (PIL) 全面指南:PIL基础入门-图像合成与处理技巧
  • 基于 Python asyncio 和币安 WebSocket 打造高频加密货币预警机器人
  • TypeScript: Reflect.ownKeys 操作(针对 Symbol)
  • Lenovo C225 一体机拆机维修教程
  • 2025牛客暑期多校训练营4 G Ghost in the Parentheses 题解记录
  • LoRA三种不同训练流程在配置和保存权重的差异(64)
  • 《Shell 大道:筑基篇(下)—— 流控筑根基,数据任驱驰》
  • shell学习笔记-实战:创建、运行与变量操作
  • Python Imaging Library (PIL) 全面指南:PIL基础入门-构建简易图像编辑器
  • matlab利用模糊算法控制PID参数实现模糊控制
  • LabVIEW实现跨 VI 簇按钮控制功能
  • 2026届大数据毕业设计选题推荐-基于Python的出行路线规划与推荐系统 爬虫数据可视化分析
  • 如何为在线医疗问诊小程序实现音视频通话功能?
  • Spring Boot整合MyBatis Plus实现多维度数据权限控制
  • Day17_【机器学习—在线数据集 鸢尾花案例】
  • Java面试实战:Spring Boot微服务在电商场景的技术深度解析
  • Scikit-learn Python机器学习 - Scikit-learn介绍
  • Dify1.8.0更新,Docker 无法拉取镜像怎么破?
  • 【大模型13】 视觉大模型与多模态
  • 部署2.516.2版本的jenkins,同时适配jdk8