Răsfoiți Sursa

产业供地会签单导出改为从模板读取,不由代码生成

huZhiHao 5 ani în urmă
părinte
comite
ba45a9dc8f

+ 0 - 2
src/main/java/platform/common/util/AlibabaSMSUtil.java

@@ -130,8 +130,6 @@ public class AlibabaSMSUtil {
         //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
         sendRequest.setOutId("yourOutId");
 
-        String s = sendRequest.getTemplateParam();
-
         //hint 此处可能会抛出异常,注意catch
         SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(sendRequest);
 

+ 36 - 0
src/main/java/platform/common/util/BokeWordUtils.java

@@ -80,6 +80,42 @@ public class BokeWordUtils {
 
     }
 
+    /**
+     * 根据模板生成新word文档
+     * 判断表格是需要替换还是需要插入,判断逻辑有$为替换,表格无$为插入
+     * author:hzh
+     * description:改为下载
+     * @param inputUrl 模板存放地址
+     * @param response 新文档存放地址
+     * @param textMap 需要替换的信息集合
+     * @param tableList 需要插入的表格信息集合
+     */
+    public static boolean changWord(String inputUrl, HttpServletResponse response,
+                                    Map<String, String> textMap, List<String[]> tableList) {
+        //模板转换默认成功
+        boolean changeFlag = true;
+        try {
+            //获取docx解析对象
+            XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));
+            //解析替换文本段落对象
+            BokeWordUtils.changeText(document, textMap);
+            //解析替换表格对象
+            BokeWordUtils.changeTable(document, textMap, tableList);
+            //生成新的word
+
+            OutputStream os = response.getOutputStream();
+            document.write(os);
+            os.close();
+            System.out.println("成功生成!");
+        } catch (IOException e) {
+            e.printStackTrace();
+            changeFlag = false;
+        }
+
+        return changeFlag;
+
+    }
+
 
     /**
      * 输出到客户端

+ 70 - 0
src/main/java/platform/modules/government/service/IndustrialLandSupplyDetailService.java

@@ -4,9 +4,12 @@ import com.alibaba.fastjson.JSONObject;
 import com.aliyuncs.exceptions.ClientException;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
+import com.xiaoleilu.hutool.util.StrUtil;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
@@ -17,6 +20,7 @@ import platform.common.exception.BaseException;
 import platform.common.util.*;
 import platform.common.util.word.ExportWord;
 import platform.modules.company.dto.LandSupplyApprovalDto;
+import platform.modules.company.entity.ProjectApplication;
 import platform.modules.government.dao.IndustrialLandSupplyDetailDao;
 import platform.modules.government.dto.DepartmentDto;
 import platform.modules.government.dto.IndustrialLandSupplyDto;
@@ -28,7 +32,9 @@ import tk.mybatis.mapper.entity.Example;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
+import java.io.File;
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 import java.text.ParseException;
 import java.util.*;
@@ -90,6 +96,11 @@ public class IndustrialLandSupplyDetailService extends BaseService<IndustrialLan
 
     private static String CONSULTATION_MESSAGE = "请反馈明确意见";
 
+    @Value("${repositoryPath}")
+    public String repositoryPath;
+
+    final private String attachmentType = "annex_template";
+
     /**
      * @Author: huZhiHao
      * @Description: 分页查询
@@ -3702,4 +3713,63 @@ public class IndustrialLandSupplyDetailService extends BaseService<IndustrialLan
 
         ew.exportCheckWord(dataList, document, response);
     }
+
+    public void exportCountersignWord2(int id, HttpServletResponse response) throws Exception {
+
+
+        IndustrialLandSupplyDetail entity = this.findById(id);
+
+        generateWord(entity, response);
+    }
+
+    private void generateWord(IndustrialLandSupplyDetail entity, HttpServletResponse response) throws Exception {
+        String time = DateUtil.getTimeString(new Date()).split(" ")[0];
+        final String name = "苏州国家高新区产业用地预审办文单" + "-" + time + ".docx";
+//        String templateUrl = repositoryPath + "/docs/annex_template/annex4.docx";
+        String templateUrl = "template/苏州高新区产业用地预审办文单.docx";
+        //文件数据库存储路径
+//        String outputUrl = getFilePath(name, "docs") + ".docx";
+
+        String title = entity.getTitle();
+        String content = entity.getContent_text();
+        String location = entity.getLocation();
+        String consultationSummary = entity.getConsultation_summary_text();
+
+        Map<String, String> dataMap = new HashMap<>();
+        dataMap.put("title", title == null ? "" : title);
+        dataMap.put("content", content == null ? "" : content);
+        dataMap.put("location", location == null ? "" : location);
+        dataMap.put("consultationSummary", consultationSummary == null ? "" : consultationSummary);
+        dataMap.put("time", time);
+
+        response.reset();
+        response.setContentType("application/octet-stream; charset=utf-8");
+        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("苏州国家高新区产业用地预审办文单" + "-" + time + ".docx", "UTF-8"));
+
+        changWord(templateUrl, response, dataMap);
+    }
+
+    public String getFilePath(String sourceFileName, String typeFolder) {
+        String baseFolder = repositoryPath + File.separator + typeFolder;
+        Date nowDate = new Date();
+        // yyyy/MM/dd
+        String fileFolder = baseFolder + File.separator + com.xiaoleilu.hutool.date.DateUtil.format(nowDate, "yyyy")
+                + File.separator + com.xiaoleilu.hutool.date.DateUtil.format(nowDate, "MM") + File.separator
+                + com.xiaoleilu.hutool.date.DateUtil.format(nowDate, "dd");
+
+        File file = new File(fileFolder);
+
+        if (!file.isDirectory()) {
+            // 如果目录不存在,则创建目录
+            file.mkdirs();
+        }
+
+        // 生成新的文件名
+        String fileName = com.xiaoleilu.hutool.date.DateUtil.format(nowDate, "yyyyMMddHHmmss") + sourceFileName;
+        return fileFolder + File.separator + fileName;
+    }
+
+    private void changWord(String annexOneTempUrl, HttpServletResponse response, Map<String, String> dataMap) {
+        BokeWordUtils.changWord(annexOneTempUrl, response, dataMap, null);
+    }
 }

+ 2 - 2
src/main/java/platform/modules/government/web/IndustrialLandSupplyController.java

@@ -553,9 +553,9 @@ public class IndustrialLandSupplyController extends BaseController {
 
     //导出会签单
     @RequestMapping("/management_committee_leader_export/{id}")
-    public void managementCommitteeLeaderExport(@PathVariable("id") int id, HttpServletResponse response) throws IOException {
+    public void managementCommitteeLeaderExport(@PathVariable("id") int id, HttpServletResponse response) throws Exception {
 
-        industrialLandSupplyDetailService.exportCountersignWord(id, response);
+        industrialLandSupplyDetailService.exportCountersignWord2(id, response);
     }
 
 //    @RequestMapping("/management_committee_leader_export/{id}")

+ 47 - 0
src/test/java/com/WordExport5.java

@@ -0,0 +1,47 @@
+package com;
+
+import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
+import org.apache.poi.xwpf.usermodel.*;
+import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute;
+import org.junit.Before;
+import org.junit.Test;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import platform.modules.government.service.IndustrialLandSupplyDetailService;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.math.BigInteger;
+
+/**
+ * @Author :huZhiHao
+ * @Date :Created in 2020/4/7 16:46
+ * @Description: 测试读取模板导出
+ * @Modified By:
+ * @Version: v0.0.1
+ */
+public class WordExport5 {
+
+    @Autowired
+    private IndustrialLandSupplyDetailService industrialLandSupplyDetailService;
+
+    private MockHttpServletRequest request;
+    private MockHttpServletResponse response;
+
+    @Before
+    public void setUp() {
+        request = new MockHttpServletRequest();
+        request.setCharacterEncoding("UTF-8");
+        response = new MockHttpServletResponse();
+    }
+
+
+    @Test
+    public void test() throws Exception {
+
+        industrialLandSupplyDetailService.exportCountersignWord2(2470, response);
+    }
+}

BIN
template/苏州高新区产业用地预审办文单.docx