zzw
2023-11-16 0117a966938d2f689e90eee907bd12bc9e123a18
core/src/main/java/com/zt/life/export/service/WordFileService.java
@@ -8,6 +8,7 @@
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
import com.zt.life.export.dto.WordFile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@@ -19,16 +20,19 @@
@Service
public class WordFileService {
    public void exportWordFile(HttpServletRequest request, WordFile wordFile, HttpServletResponse response) throws UnsupportedEncodingException, FileNotFoundException {
    @Value("${zt.oss.local-path}")
    private String localPath;
    public void exportWordFile(HttpServletRequest request, Object dataObj, WordFile wordFile, HttpServletResponse response) throws UnsupportedEncodingException, FileNotFoundException {
        // 数据map集合
        Map<String, Object> data = new HashMap<>();
        Map<String, Object> wordData = new HashMap<>();
        //HackLoopTableRenderPolicy hackLoopTableRenderPolicy = new HackLoopTableRenderPolicy();
        Configure config = null;
        ConfigureBuilder builder = Configure.newBuilder();
        this.formatWordData(wordFile, data, builder);
        this.formatWordData(dataObj, wordData, builder);
        config = builder.build();
        File fl = new File(wordFile.getModulePath());
        File fl = new File(localPath+"/template/"+wordFile.getModulePath());
        FileInputStream fs = new FileInputStream(fl);
        XWPFTemplate template;
        if (config != null) {
@@ -36,22 +40,22 @@
        } else {
            template = XWPFTemplate.compile(fs);
        }
        template.render(data);
        template.render(wordData);
        DownloadService.download(request, response, template, wordFile.getWordName());
    }
    public void formatWordData(Object obj, Map<String, Object> data, ConfigureBuilder builder) {
        Field[] fields = ReflectUtil.getFields(obj.getClass());
    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(obj, field.getName()); // 属性对应的数据
            Object staticFieldValue = ReflectUtil.getFieldValue(dataObj, field.getName()); // 属性对应的数据
            if (typeName.contains("com.zt.life.modules")) {
                formatWordData(staticFieldValue, data, builder);
                formatWordData(staticFieldValue, wordData, builder);
            } else {
                if (field.getType().getName().equals("java.util.List")) {
                    builder.bind(fieldName, new HackLoopTableRenderPolicy());
                   builder.bind(fieldName, new HackLoopTableRenderPolicy());
                } else if (staticFieldValue != null) {
                    String valStr = staticFieldValue.toString();
                    if (valStr.contains("签名图片:")) {
@@ -60,8 +64,8 @@
                        /*put("localbyte", new PictureRenderData(80, 100, ".png", new FileInputStream("./logo.png")));*/
                    }
                }
                if (data.get(fieldName) == null)
                    data.put(fieldName, staticFieldValue);
                if (wordData.get(fieldName) == null)
                    wordData.put(fieldName, staticFieldValue);
            }
        }
    }