diff --git a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/domain/WordExportTest.java b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/domain/WordExportTest.java index 7894e7d..af4f20c 100644 --- a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/domain/WordExportTest.java +++ b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/domain/WordExportTest.java @@ -13,6 +13,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; +import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -56,9 +57,13 @@ public class WordExportTest { // 4. ✅ 使用 EasyPOI 填充模板(传 File) XWPFDocument document = WordExportUtil.exportWord07(String.valueOf(tempFile), data); - + List descriptions = Arrays.asList( + "这是第一张图片的描述", + "这是第二张图片的描述", + "这是第三张图片的描述" + ); // 5. ✅ 在文档末尾追加图片 - appendImages(document, IMAGE_PATHS); + appendImages(document, IMAGE_PATHS, descriptions); // 5. 导出到本地 @@ -74,34 +79,54 @@ public class WordExportTest { System.out.println("✅ Word 文件已生成:" + outputPath); } - // ✅ 新增方法:追加图片到文档末尾 - private static void appendImages(XWPFDocument document, List imagePaths) throws Exception { + private static void appendImages(XWPFDocument document, List imagePaths, List descriptions) throws Exception { if (imagePaths == null || imagePaths.isEmpty()) { System.out.println("📭 无图片需要插入"); return; } - for (String imagePath : imagePaths) { + // 确保描述的数量不超过图片数量 + if (descriptions != null && descriptions.size() > imagePaths.size()) { + descriptions = descriptions.subList(0, imagePaths.size()); + } + + for (int i = 0; i < imagePaths.size(); i++) { + String imagePath = imagePaths.get(i); File imageFile = new File(imagePath); if (!imageFile.exists()) { System.err.println("⚠️ 图片未找到:" + imagePath); continue; } - // 创建新段落 - XWPFParagraph paragraph = document.createParagraph(); - paragraph.setAlignment(ParagraphAlignment.CENTER); // 居中 - XWPFRun run = paragraph.createRun(); + // 创建新段落用于图片(居中) + XWPFParagraph imgParagraph = document.createParagraph(); + imgParagraph.setAlignment(ParagraphAlignment.CENTER); // 居中 + XWPFRun imgRun = imgParagraph.createRun(); // 读取图片 try (FileInputStream is = new FileInputStream(imageFile)) { int pictureType = getPictureType(imageFile.getName()); - run.addPicture(is, pictureType, imageFile.getName(), + // 设置图片大小,这里使用300x150 EMU + imgRun.addPicture(is, pictureType, imageFile.getName(), Units.toEMU(300), Units.toEMU(150)); } + // 如果提供了描述,则在图片下方添加描述段落 + if (descriptions != null && i < descriptions.size()) { + String desc = descriptions.get(i); + + XWPFParagraph captionParagraph = document.createParagraph(); + captionParagraph.setAlignment(ParagraphAlignment.CENTER); // 描述也居中 + + XWPFRun captionRun = captionParagraph.createRun(); + captionRun.setText("图" + (i + 1) + ":" + desc); // 使用描述内容 + captionRun.setColor("666666"); // 灰色文字,更像图注 + captionRun.setFontSize(10); // 字体小一点 + captionRun.setItalic(true); // 斜体(可选) + captionRun.setFontFamily("宋体"); // 设置字体 + } - // 每张图后空一行 + // 每张图和描述后空一行 document.createParagraph(); } } @@ -223,27 +248,27 @@ public class WordExportTest { data.put("o_time", "4.2"); // 用户手动填写,单位:min,不参与任何计算 //----------PM10 测试---------- - double pm10Display = 0.8; - double pm10Std = 0.78; + double pmTenDisplay = 0.8; + double pmTenStd = 0.78; - data.put("pm10_display", String.valueOf(pm10Display)); - data.put("pm10_std", String.valueOf(pm10Std)); - data.put("pm10_error", calcError(pm10Display, pm10Std)); // 自动计算:相对误差 + data.put("pm_ten_display", String.valueOf(pmTenDisplay)); + data.put("pm_ten_std", String.valueOf(pmTenStd)); + data.put("pm_ten_error", calcError(pmTenDisplay, pmTenStd)); // 自动计算:相对误差 - data.put("pm10_k", 0.8); - data.put("k0_A", 0.8); + data.put("pm_ten_k", 0.8); + data.put("slope_a", 0.8); //----------PM2 测试---------- - double pm2Display = 0.8; - double pm2Std = 0.78; + double pmTwoDisplay = 0.8; + double pmTwoStd = 0.78; - data.put("pm2_display", String.valueOf(pm2Display)); - data.put("pm2_std", String.valueOf(pm2Std)); - data.put("pm2_error", calcError(pm2Display, pm2Std)); // 自动计算:相对误差 + data.put("pm_two_display", String.valueOf(pmTwoDisplay)); + data.put("pm_two_std", String.valueOf(pmTwoStd)); + data.put("pm_two_error", calcError(pmTwoDisplay, pmTwoStd)); // 自动计算:相对误差 - data.put("pm2_k", 0.5); - data.put("k0_B", 0.5); + data.put("pm_two_k", 0.5); + data.put("slope_b", 0.5); System.out.println("✅ 数据准备完成!"); System.out.println(JSONUtil.toJsonStr(data)); diff --git a/ruoyi-modules/ruoyi-demo/src/main/resources/score_template.docx b/ruoyi-modules/ruoyi-demo/src/main/resources/score_template.docx index 92e44f8..d9a1308 100644 Binary files a/ruoyi-modules/ruoyi-demo/src/main/resources/score_template.docx and b/ruoyi-modules/ruoyi-demo/src/main/resources/score_template.docx differ