SpringBoot将数据写入word模板,生成word文档

用途:生成电子履历或者固定格式文档

依赖:

        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.30</version>
        </dependency>

 

代码:

 1 import freemarker.template.Configuration;
 2 import freemarker.template.Template;
 3 import org.springframework.core.io.ByteArrayResource;
 4 import org.springframework.core.io.InputStreamSource;
 5 
 6 import java.io.*;
 7 import java.nio.charset.StandardCharsets;
 8 import java.util.Map;
 9 
10 /**
11  * @program: world
12  * @description: doc工具类
13  * @author: XinHai.Ma
14  * @create: 2021-09-09 15:54
15  */
16 public class DocUtils {
17 
18     public static void saveWord(String filePath, Map<String,Object> dataMap) throws IOException {
19         Configuration configuration = new Configuration();
20         configuration.setDefaultEncoding("utf-8");
21         configuration.setClassForTemplateLoading(DocUtils.class, "/");
22         Template template = configuration.getTemplate("templates/template.xml");
23         InputStreamSource streamSource = createWord(template, dataMap);
24         InputStream inputStream = streamSource.getInputStream();
25         FileOutputStream outputStream = new FileOutputStream(filePath);
26         byte[] bytes = new byte[1024];
27         while ((inputStream.read(bytes)) != -1) {
28             outputStream.write(bytes);// 写入数据
29         }
30         inputStream.close();
31         outputStream.close();
32     }
33 
34     public static InputStreamSource createWord(Template template, Map<String, Object> dataMap) {
35         StringWriter out = null;
36         Writer writer = null;
37         try {
38             out = new StringWriter();
39             writer = new BufferedWriter(out, 1024);
40             template.process(dataMap, writer);
41             return new ByteArrayResource(out.toString().getBytes(StandardCharsets.UTF_8));
42         } catch (Exception e) {
43             e.printStackTrace();
44         } finally {
45             try {
46                 writer.close();
47                 out.close();
48             } catch (IOException e) {
49                 e.printStackTrace();
50             }
51         }
52         return null;
53     }
54 
55 }

 

调用代码:

 1 import com.maxinhai.world.utils.DocUtils;
 2 import org.junit.Test;
 3 import org.junit.runner.RunWith;
 4 import org.springframework.boot.test.context.SpringBootTest;
 5 import org.springframework.test.context.junit4.SpringRunner;
 6 
 7 import java.io.IOException;
 8 import java.util.HashMap;
 9 import java.util.Map;
10 
11 /**
12  * @program: world
13  * @description: doc测试用例
14  * @author: XinHai.Ma
15  * @create: 2021-09-09 16:16
16  */
17 @RunWith(SpringRunner.class)
18 @SpringBootTest
19 public class DocTests {
20 
21     @Test
22     public void test() {
23         try {
24             Map<String, Object> dataMap = new HashMap<>();
25             dataMap.put("title", "献祭之主");
26             dataMap.put("author", "王信");
27             dataMap.put("createDate", "2021年9月9日");
28             dataMap.put("title_item1", "第14章 :我父母为了保护北马城所牺牲!");
29             dataMap.put("content", "我父母为了保护北马市,死在了魔兽攻城中!");
30             DocUtils.saveWord("C:\\Users\\86186\\Documents\\献祭之主.docx", dataMap);
31         } catch (IOException e) {
32             e.printStackTrace();
33         }
34     }
35 
36 }

 

模板制作:

1. 新建一个word文档,填入固定内容;

2. 在要用程序写入数据的地方,插入-文档部件-域-自动图文集列(微软Word叫AUTOTEXTLIST),标签名和程序标签要对应;

 

 

3. 文档保存为Word XML格式;

4. 放入程序要去读取模板的路径;

 

生成文档效果:

posted @ 2021-09-09 17:00  尘世间迷茫的小书童  阅读(7095)  评论(3编辑  收藏  举报