zhangyong
2023-08-22 1353e87cb21a4032d585d7404bae9042f2ebcf08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.example.springboot;
 
import cn.hutool.core.io.FileUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.*;
import org.junit.jupiter.api.Test;
import org.springframework.util.ResourceUtils;
 
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
 
public class WordTest {
 
    @Test
    public void test1() throws IOException {
        File file = ResourceUtils.getFile("classpath:doc/test.docx");
        String imgPath = "D:\\B站\\基础讲解\\vue3\\springboot-vue3\\src\\main\\resources\\doc\\hz.png";
        String outPath = "D:\\B站\\基础讲解\\vue3\\springboot-vue3\\src\\main\\resources\\doc\\test1.docx";
        Map<String, Object> map = new HashMap<>();
 
        // 文本
        map.put("name", "张三");
        map.put("age", "20");
        map.put("edu", "本科");
 
 
        // 表格
        RowRenderData row0 = Rows.of("姓名", "年龄", "学历", "照片").textColor("ffffff")
                .bgColor("4472C4").center().create();
        RowRenderData row1 = Rows.of("张三", "20", "本科").center().create();
        RowRenderData row2 = Rows.of("李四", "22", "本科").center().create();
 
        // 单元格插入图片
        PictureRenderData pic = Pictures.ofLocal(imgPath).size(50, 50).create();
        row1.addCell(Cells.of(pic).create());
        row2.addCell(Cells.of(pic).create());
 
        TableRenderData tableRenderData = Tables.create(row0, row1);
        tableRenderData.addRow(row2);
        map.put("table", tableRenderData);
 
        // 图片  本地、流、url、bytes
        map.put("img", Pictures.ofLocal(imgPath).create());
        map.put("streamImg", Pictures.ofStream(Files.newInputStream(new File(imgPath).toPath()))
                .size(100, 100).create());
        map.put("urlImg", Pictures.ofUrl("https://img-blog.csdnimg.cn/522902589b3d4c21b30e7bc229897b41.png")
                .size(150, 150).create());
        map.put("byteImg", Pictures.ofBytes(FileUtil.readBytes(imgPath)).size(200, 200).create());
 
        XWPFTemplate template = XWPFTemplate.compile(file).render(map);
        template.writeAndClose(Files.newOutputStream(Paths.get(outPath)));
    }
 
    @Test
    public void test2() throws IOException {
        // word模板
        String path = "D:\\B站\\基础讲解\\vue3\\springboot-vue3\\src\\main\\resources\\doc\\test2.docx";
        XWPFTemplate template = XWPFTemplate.compile(path);  // 读取模板内容
        Map<String, Object> map = new HashMap<>();
        map.put("date", "2023-07-02");  // map里面的变量名称一定要跟模板里的一致
        template.render(map);
        template.writeAndClose(Files.newOutputStream(Paths.get("D:\\B站\\基础讲解\\vue3\\springboot-vue3\\src\\main\\resources\\doc\\text.docx")));
    }
 
    @Test
    public void test3() throws IOException {
        // word模板
        String path = "D:\\B站\\基础讲解\\vue3\\springboot-vue3\\src\\main\\resources\\doc\\tableTemp.docx";
        XWPFTemplate template = XWPFTemplate.compile(path);  // 读取模板内容
        Map<String, Object> map = new HashMap<>();
 
        RowRenderData row0 = Rows.of("姓名", "年龄", "头像").center().textColor("FFFFFF").bgColor("6495ED").create();
        RowRenderData row1 = Rows.of("张三", "20").center().create();
        PictureRenderData img = Pictures.of("D:\\B站\\基础讲解\\vue3\\springboot-vue3\\src\\main\\resources\\doc\\hz.png")
                .size(50, 50).create();
        CellRenderData cellRenderData = Cells.of(img).create();
        row1.addCell(cellRenderData);
 
        RowRenderData row2 = Rows.of("李四", "22").center().create();
        row2.addCell(cellRenderData);
        TableRenderData tableRenderData = Tables.create(row0, row1, row2);
 
        map.put("table", tableRenderData);  // map里面的变量名称一定要跟模板里的一致
        template.render(map);
        template.writeAndClose(Files.newOutputStream(Paths.get("D:\\B站\\基础讲解\\vue3\\springboot-vue3\\src\\main\\resources\\doc\\table.docx")));
    }
 
    @Test
    public void test4() throws IOException {
        // word模板
        String path = "D:\\B站\\基础讲解\\vue3\\springboot-vue3\\src\\main\\resources\\doc\\imgTemp.docx";
        XWPFTemplate template = XWPFTemplate.compile(path);  // 读取模板内容
 
        Map<String, Object> map = new HashMap<>();
        PictureRenderData img = Pictures.of("D:\\B站\\基础讲解\\vue3\\springboot-vue3\\src\\main\\resources\\doc\\hz.png").create();
        PictureRenderData streamImg = Pictures.ofStream(Files.newInputStream(Paths.get("D:\\B站\\基础讲解\\vue3\\springboot-vue3\\src\\main\\resources\\doc\\hz.png"))).create();
        PictureRenderData urlImg = Pictures.ofUrl("https://img-blog.csdnimg.cn/522902589b3d4c21b30e7bc229897b41.png").create();
 
 
        map.put("img", img);  // map里面的变量名称一定要跟模板里的一致
        map.put("streamImg", streamImg);
        map.put("urlImg", urlImg);
        template.render(map);
        template.writeAndClose(Files.newOutputStream(Paths.get("D:\\B站\\基础讲解\\vue3\\springboot-vue3\\src\\main\\resources\\doc\\img.docx")));
    }
 
}