whycxzp
2022-12-05 bac7755aa3aea1705caf870c4318803186484e9c
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
package com.whyc.util;
 
import com.deepoove.poi.XWPFTemplate;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
 
public class WordUtil {
 
    public static void fillValue(String inFilePath, String outFilePath, List valueList) throws IOException {
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("fzr", valueList.get(0));
        hashMap.put("bz", valueList.get(1));
        hashMap.put("zz", valueList.get(2));
        hashMap.put("pb", valueList.get(3));
        XWPFTemplate template = XWPFTemplate.compile(inFilePath).render(hashMap);
        template.write(new FileOutputStream(outFilePath));
 
 
        /*//获取临时文件
        File file = new File(filePath);
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Configure config = Configure.newBuilder().bind("detailList", policy).build();
        XWPFTemplate template = XWPFTemplate.compile(filePath, config).render(
                new HashMap<String, Object>() {{
                    put("fzr", "负责人");
                    put("bz", "编制");
                    put("zz", "制造");
                    put("pb", "品保");
                }}
        );
        //生成文件名
        Long time = new Date().getTime();
        // 生成的word格式
        String fileName =  time + ".docx";
        System.out.println("文件名:" + fileName);
        //=================生成word到设置浏览默认下载地址=================
        //解决文件下载名称变为 ____下划线的BUG
        //针对IE或者以IE为内核的浏览器:
        String userAgent = request.getHeader("User-Agent");
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName = java.net.URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
        } else {
            //非IE浏览器:
            fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            // 设置强制下载不打开
            response.setContentType("application/force-download");
            // 设置文件名
            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
            response.setCharacterEncoding("UTF-8");
            OutputStream out = response.getOutputStream();
            template.write(out);
            out.flush();
            out.close();
            template.close();
        }*/
    }
}