From 5d8c028ff37838e0628d5852e41b19efc80e4be1 Mon Sep 17 00:00:00 2001
From: jinlin <jinlin>
Date: 星期一, 27 十一月 2023 15:09:41 +0800
Subject: [PATCH] Changes

---
 core/src/main/java/com/zt/life/export/service/WordFileService.java |   95 +++++++++++++++++------------------------------
 1 files changed, 34 insertions(+), 61 deletions(-)

diff --git a/core/src/main/java/com/zt/life/export/service/WordFileService.java b/core/src/main/java/com/zt/life/export/service/WordFileService.java
index 810b2a2..2329e9e 100644
--- a/core/src/main/java/com/zt/life/export/service/WordFileService.java
+++ b/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,73 +20,19 @@
 
 @Service
 public class WordFileService {
+    @Value("${zt.oss.local-path}")
+    private String localPath;
 
-    public void export(HttpServletRequest request, WordFile wordFile, HttpServletResponse response) throws IOException {
-
-        Map<String, Object> datas = new HashMap<String, Object>();
-        // 灏唚ord鍐呭json瀛楃涓茶浆鎴恓son瀵硅薄
-
-        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();
-            }
-        }
-
-        // 涓嶆敮鎸乨oc鐨勬牸寮�
-        // 鍔犺浇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 {
-
+    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(dataObj, wordData, builder);
 
-        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());
+        File fl = new File(localPath+"/template/"+wordFile.getModulePath());
         FileInputStream fs = new FileInputStream(fl);
         XWPFTemplate template;
         if (config != null) {
@@ -93,7 +40,33 @@
         } else {
             template = XWPFTemplate.compile(fs);
         }
-        template.render(data);
+        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) {
+                    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")));*/
+                    }
+                }
+                if (wordData.get(fieldName) == null)
+                    wordData.put(fieldName, staticFieldValue);
+            }
+        }
+    }
 }

--
Gitblit v1.9.1