diff --git a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/SiteEvaluationInfoController.java b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/SiteEvaluationInfoController.java index 444b93b..c303688 100644 --- a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/SiteEvaluationInfoController.java +++ b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/SiteEvaluationInfoController.java @@ -109,4 +109,9 @@ public class SiteEvaluationInfoController extends BaseController { public void download( HttpServletResponse response) throws Exception { siteEvaluationInfoService.download(response); } + + @GetMapping("/download/world2") + public void download2( HttpServletResponse response) throws Exception { + siteEvaluationInfoService.download2(response); + } } diff --git a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/service/ISiteEvaluationInfoService.java b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/service/ISiteEvaluationInfoService.java index b1c6b1e..ac23387 100644 --- a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/service/ISiteEvaluationInfoService.java +++ b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/service/ISiteEvaluationInfoService.java @@ -7,6 +7,7 @@ import org.dromara.demo.domain.bo.SiteEvaluationInfoBo; import org.dromara.common.mybatis.core.page.TableDataInfo; import org.dromara.common.mybatis.core.page.PageQuery; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.Collection; import java.util.List; @@ -70,4 +71,6 @@ public interface ISiteEvaluationInfoService { Boolean deleteWithValidByIds(Collection ids, Boolean isValid); void download(HttpServletResponse response) throws Exception; + + void download2(HttpServletResponse response) throws Exception; } diff --git a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/service/impl/SiteEvaluationInfoServiceImpl.java b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/service/impl/SiteEvaluationInfoServiceImpl.java index bf65661..996868d 100644 --- a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/service/impl/SiteEvaluationInfoServiceImpl.java +++ b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/service/impl/SiteEvaluationInfoServiceImpl.java @@ -237,6 +237,67 @@ public class SiteEvaluationInfoServiceImpl implements ISiteEvaluationInfoService try (templateIs) { // 3. 创建临时文件 → 填充模板 File tempFile = File.createTempFile("temp", ".docx"); + // 告诉 JVM:当程序正常退出时(JVM 关闭),自动删除这个临时文件。 + tempFile.deleteOnExit(); + Files.copy(templateIs, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + + XWPFDocument document = WordExportUtil.exportWord07(tempFile.getAbsolutePath(), data); + AttachmentVo attachmentVo = new AttachmentVo(); + List attachmentList = new ArrayList<>(); + + Attachment attachment1 = new Attachment(); + attachment1.setDescription("描述1"); + attachment1.setOssId("1964745602352324609"); + attachment1.setName("故障图片.png"); + attachmentList.add(attachment1); + + Attachment attachment2 = new Attachment(); + attachment2.setDescription("描述2"); + attachment2.setOssId("1964745602352324609"); + attachment2.setName("故障图片.png"); + attachmentList.add(attachment2); + attachmentVo.setAttachmentList(attachmentList); + + appendImages(document,attachmentVo ,ossService); + + // ✅ 4. 设置响应头 + String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"); + + response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); + response.setHeader("Content-Disposition", + "attachment1; filename=\"" + encodedFileName + "\"; filename*=UTF-8''" + encodedFileName); + response.setCharacterEncoding("UTF-8"); + + // ✅ 5. 写入输出流 + document.write(response.getOutputStream()); + + // ✅ 6. 关闭 document + document.close(); + + // ✅ 7. 强制刷新输出缓冲区(关键!) + response.flushBuffer(); + } + // ✅ try 结束前,数据已发出 + } + + @Override + public void download2(HttpServletResponse response) throws Exception { + // 1. 准备数据 + Map data = getHumanData(); + String siteName = (String) data.get("siteName"); + String fileName = siteName + "防人为干扰检查表.docx"; + + // 2. 加载模板 + InputStream templateIs = this.getClass().getClassLoader() + .getResourceAsStream("human_template.docx"); + if (templateIs == null) { + throw new FileNotFoundException("模板未找到"); + } + + try (templateIs) { + // 3. 创建临时文件 → 填充模板 + File tempFile = File.createTempFile("temp", ".docx"); + // 告诉 JVM:当程序正常退出时(JVM 关闭),自动删除这个临时文件。 tempFile.deleteOnExit(); Files.copy(templateIs, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); @@ -385,7 +446,6 @@ public class SiteEvaluationInfoServiceImpl implements ISiteEvaluationInfoService }; } - @NotNull private static Map getData() { // 1. 准备数据(用户填写的内容) Map data = new LinkedHashMap<>(); @@ -522,6 +582,42 @@ public class SiteEvaluationInfoServiceImpl implements ISiteEvaluationInfoService return data; } + + private static Map getHumanData() { + // 1. 准备数据(用户填写的内容) + Map data = new LinkedHashMap<>(); + data.put("siteName", "临安区龙岗镇站"); + data.put("operationUnit", "安徽蓝盾光电子股份有限公司"); + // ✅ 自动生成当前时间,并格式化为 "yyyy-MM-dd HH:mm:ss" + String currentDateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + data.put("checkDateTime", currentDateTime); + data.put("supervisor", "北京国研咨询"); + data.put("checkPeople", "李工"); + data.put("result", "无干扰"); + data.put("yes_1", "√"); + data.put("yes_2", "√"); + data.put("yes_3", "√"); + data.put("yes_4", "√"); + data.put("no_1", "√"); + data.put("no_2", "√"); + data.put("no_3", "√"); + data.put("no_4", "√"); + data.put("trouble_1", "存在问题"); + data.put("trouble_2", "存在问题"); + data.put("trouble_3", "存在问题"); + data.put("trouble_4", "存在问题"); + data.put("remark_1", "备注测试"); + data.put("remark_2", "备注测试"); + data.put("remark_3", "备注测试"); + data.put("remark_4", "备注测试"); + + System.out.println("✅ 数据准备完成!"); + System.out.println(JSONUtil.toJsonStr(data)); + JSONObject jsonObject = JSONUtil.parseObj(data); + System.out.println(jsonObject.toStringPretty()); + return data; + } + // 工具方法:计算相对误差,保留1位小数 private static String calcError(double mfcFlow, double stdFlow) { if (Math.abs(stdFlow) < 0.0001) return "0.0"; // 防止除0 diff --git a/ruoyi-modules/ruoyi-demo/src/main/resources/human_template.docx b/ruoyi-modules/ruoyi-demo/src/main/resources/human_template.docx new file mode 100644 index 0000000..c6a4f0e Binary files /dev/null and b/ruoyi-modules/ruoyi-demo/src/main/resources/human_template.docx differ