jinlin
2024-11-19 195bb5267a6ece13363303e177fee7d1fa3941aa
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
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 com.zt.life.oss.OssEncryptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
 
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;
 
@Service
public class WordFileService {
    @Value("${zt.oss.local-path}")
    private String localPath;
 
    @Autowired
    private OssEncryptService ossEncryptService;
 
 
    public void exportWordFile(HttpServletRequest request, Object dataObj, WordFile wordFile, HttpServletResponse response) throws UnsupportedEncodingException, FileNotFoundException {
        // 数据map集合
        Map<String, Object> wordData = new HashMap<>();
        //HackLoopTableRenderPolicy hackLoopTableRenderPolicy = new HackLoopTableRenderPolicy();
        Configure config = null;
        ConfigureBuilder builder = Configure.newBuilder();
        this.formatWordData(dataObj, wordData, builder);
 
        config = builder.build();
        File fl = new File(localPath + "/template/" + wordFile.getModulePath());
        FileInputStream fs = new FileInputStream(fl);
        XWPFTemplate template;
        if (config != null) {
            template = XWPFTemplate.compile(fs, config);
        } else {
            template = XWPFTemplate.compile(fs);
        }
        template.render(wordData);
        DownloadService.download(request, response, template, wordFile.getWordName());
    }
 
    public void formatWordData(Object dataObj, Map<String, Object> wordData, ConfigureBuilder builder) {
        Field[] fields = ReflectUtil.getFields(dataObj.getClass());
        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            String typeName = field.getType().getName();
            Object staticFieldValue = ReflectUtil.getFieldValue(dataObj, field.getName()); // 属性对应的数据
            if (typeName.contains("com.zt.life.modules")) {
                formatWordData(staticFieldValue, wordData, builder);
            } else {
                if (field.getType().getName().equals("java.util.List")) {
                    builder.bind(fieldName, new HackLoopTableRenderPolicy());
                } else if (staticFieldValue != null) {
                    if (typeName.contains("java.util.Date") && fieldName.contains("Date")) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
                        staticFieldValue = sdf.format(staticFieldValue);
                    }
                    String valStr = staticFieldValue.toString();
                    if (valStr.contains("文件图片:")) {
                        valStr = localPath + valStr.replace("文件图片:", "");
                        File file = new File(valStr);
                        if (file.exists() && !localPath.equals(valStr)) {
                            try (InputStream in = ossEncryptService.decrypt(file)) {
                                InputStream in2 = ossEncryptService.decrypt(file);
                                BufferedImage bufferedImage = ImageIO.read(in2);
                                int width = bufferedImage.getWidth();
                                int height = bufferedImage.getHeight();
                                if (width > 900 ) {
                                    height = (height * 900) / width;
                                    staticFieldValue = new PictureRenderData(900, height, ".png", in);
                                } else {
                                    staticFieldValue = new PictureRenderData(width, height, ".png", in);
                                }
                                /*staticFieldValue = new PictureRenderData(80, 100, "d://" + valStr);*/
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        } else {
                            staticFieldValue = new PictureRenderData(100, 30, localPath + "template/noSign.png");
                        }
                    }
                }
                if (wordData.get(fieldName) == null)
                    wordData.put(fieldName, staticFieldValue);
            }
            if (wordData.get(fieldName) == null)
                wordData.put(fieldName, staticFieldValue);
        }
    }
}