|
|
@ -2,16 +2,30 @@ package org.dromara.demo.domain; |
|
|
|
|
|
|
|
import cn.afterturn.easypoi.word.WordExportUtil; |
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
import org.apache.poi.util.Units; |
|
|
|
import org.apache.poi.xwpf.usermodel.ParagraphAlignment; |
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument; |
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFParagraph; |
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFRun; |
|
|
|
import org.jetbrains.annotations.NotNull; |
|
|
|
|
|
|
|
import java.io.*; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.util.LinkedHashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
public class WordExportTest { |
|
|
|
|
|
|
|
// ✅ 假设这是你要插入的图片路径列表(可以来自数据库、文件夹扫描等)
|
|
|
|
private static final List<String> IMAGE_PATHS = List.of( |
|
|
|
"D:\\Users\\14212\\Desktop\\故障图片.png", |
|
|
|
"D:\\Users\\14212\\Desktop\\故障图片.png", |
|
|
|
"D:\\Users\\14212\\Desktop\\故障图片.png" |
|
|
|
// 可以是 0 个、1 个、N 个
|
|
|
|
); |
|
|
|
public static void main(String[] args) throws Exception { |
|
|
|
// 1. 准备数据(用户填写的内容)
|
|
|
|
Map<String, Object> data = getData(); |
|
|
@ -43,6 +57,10 @@ public class WordExportTest { |
|
|
|
// 4. ✅ 使用 EasyPOI 填充模板(传 File)
|
|
|
|
XWPFDocument document = WordExportUtil.exportWord07(String.valueOf(tempFile), data); |
|
|
|
|
|
|
|
// 5. ✅ 在文档末尾追加图片
|
|
|
|
appendImages(document, IMAGE_PATHS); |
|
|
|
|
|
|
|
|
|
|
|
// 5. 导出到本地
|
|
|
|
String outputPath = "D:\\Users\\14212\\Desktop\\评分报告.docx"; // 可改为你的路径
|
|
|
|
try (FileOutputStream out = new FileOutputStream(outputPath)) { |
|
|
@ -56,6 +74,50 @@ public class WordExportTest { |
|
|
|
System.out.println("✅ Word 文件已生成:" + outputPath); |
|
|
|
} |
|
|
|
|
|
|
|
// ✅ 新增方法:追加图片到文档末尾
|
|
|
|
private static void appendImages(XWPFDocument document, List<String> imagePaths) throws Exception { |
|
|
|
if (imagePaths == null || imagePaths.isEmpty()) { |
|
|
|
System.out.println("📭 无图片需要插入"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
for (String imagePath : imagePaths) { |
|
|
|
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(); |
|
|
|
|
|
|
|
// 读取图片
|
|
|
|
try (FileInputStream is = new FileInputStream(imageFile)) { |
|
|
|
int pictureType = getPictureType(imageFile.getName()); |
|
|
|
run.addPicture(is, pictureType, imageFile.getName(), |
|
|
|
Units.toEMU(300), Units.toEMU(150)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 每张图后空一行
|
|
|
|
document.createParagraph(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// ✅ 根据文件扩展名判断图片类型
|
|
|
|
private static int getPictureType(String filename) { |
|
|
|
String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase(); |
|
|
|
return switch (ext) { |
|
|
|
case "png" -> XWPFDocument.PICTURE_TYPE_PNG; |
|
|
|
case "jpg", "jpeg" -> XWPFDocument.PICTURE_TYPE_JPEG; |
|
|
|
case "gif" -> XWPFDocument.PICTURE_TYPE_GIF; |
|
|
|
case "bmp" -> XWPFDocument.PICTURE_TYPE_BMP; |
|
|
|
default -> XWPFDocument.PICTURE_TYPE_JPEG; |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
@NotNull |
|
|
|
private static Map<String, Object> getData() { |
|
|
|
// 1. 准备数据(用户填写的内容)
|
|
|
|