jinlin
2023-11-03 35435e8b1995e6775c82b86652381e07e3faff54
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
package com.zt.life.export.service;
 
import cn.hutool.core.util.ReflectUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
 
import com.zt.life.export.dto.WordFile;
import org.springframework.stereotype.Service;
 
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.util.*;
 
@Service
public class WordFileService {
 
    public void export(HttpServletRequest request, WordFile wordFile, HttpServletResponse response) throws IOException {
 
        Map<String, Object> datas = new HashMap<String, Object>();
        // 将word内容json字符串转成json对象
 
        Field[] fields = ReflectUtil.getFields(wordFile.getClass());
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                //Object value = field.get(wordFile);
                datas.put(field.getName(), field.get(wordFile));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
 
        // 不支持doc的格式
        // 加载word导出模板,读入导出内容
 
        XWPFTemplate template = XWPFTemplate.compile(wordFile.getModulePath());
        template.render(datas);
        //FileOutputStream out = new FileOutputStream("D:\\out_template.docx");
        //template.write(out);
        //out.flush();
        //out.close();
        //template.close();
        DownloadService.download(request, response, template, wordFile.getWordName());
    }
 
 
    /**
     * 打印word(单个表格数据)
     *
     * @param request
     * @param wordFile
     * @param response
     */
    public void exportForTables(HttpServletRequest request, WordFile wordFile, HttpServletResponse response) throws UnsupportedEncodingException, FileNotFoundException {
 
        // 数据map集合
        Map<String, Object> data = new HashMap<>();
        //HackLoopTableRenderPolicy hackLoopTableRenderPolicy = new HackLoopTableRenderPolicy();
        Configure config = null;
        ConfigureBuilder builder = Configure.newBuilder();
 
        Field[] fields = ReflectUtil.getFields(wordFile.getClass());
        for (Field field : fields) {
            field.setAccessible(true); // 对私有属性进行访问
            String fieldName = field.getName(); // 属性名
            Object staticFieldValue = ReflectUtil.getFieldValue(wordFile, field.getName()); // 属性对应的数据
            if (field.getType().getName().equals("java.util.List")) {//如果该属性是list类型
                builder.bind(fieldName, new HackLoopTableRenderPolicy());
            }else if(staticFieldValue!=null) {
                String valStr = staticFieldValue.toString();
                if (valStr.contains("签名图片:")) {
                    valStr = valStr.replace("签名图片:","");
                    staticFieldValue = new PictureRenderData(80, 100, "d://" + valStr);
                    /*            put("localbyte", new PictureRenderData(80, 100, ".png", new FileInputStream("./logo.png")));
                     */
                }
            }
            data.put(fieldName, staticFieldValue);
            data.put(fieldName, staticFieldValue);
        }
        config = builder.build();
        File fl = new File(wordFile.getModulePath());
        FileInputStream fs = new FileInputStream(fl);
        XWPFTemplate template;
        if (config != null) {
            template = XWPFTemplate.compile(fs, config);
        } else {
            template = XWPFTemplate.compile(fs);
        }
        template.render(data);
        DownloadService.download(request, response, template, wordFile.getWordName());
    }
}