瀏覽代碼

添加word导出模版

yuliang 5 年之前
父節點
當前提交
dadb383ff1

+ 192 - 0
whepi-web/src/main/java/com/bofeng/controller/EpiLYExportController.java

@@ -0,0 +1,192 @@
+package com.bofeng.controller;
+
+import com.bofeng.word.WordUtils;
+import com.google.common.collect.Lists;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@RestController
+public class EpiLYExportController {
+
+    @ApiOperation("导出单个健康及旅居申报表")
+    @GetMapping("/whepi/export/lyReport")
+    public @ResponseBody
+    void exportSellPlan(HttpServletRequest request, HttpServletResponse response) {
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("userName", "张山");
+        map.put("workPlace", "武汉");
+        map.put("telPhone", "13245452345");
+        map.put("isLeave", "是");
+        map.put("trip", "北京、上海、武汉");
+        map.put("backTime", "2020-03-10");
+        map.put("vehicle", "火箭");
+
+        Map<String, Object> rt = new HashMap<String, Object>();
+        map.put("rt", rt);
+        rt.put("d1", "02-20");
+        rt.put("d2", "02-21");
+        rt.put("d3", "02-22");
+        rt.put("d4", "02-23");
+        rt.put("d5", "02-24");
+        rt.put("t1", "-");
+        rt.put("t2", "37.0");
+        rt.put("t3", "36.6");
+        rt.put("t4", "36.6");
+        rt.put("t5", "36.8");
+
+        map.put("otherSymptom", "ee");
+        map.put("isTrue", "否");
+
+        WordUtils.exportWord("word/健康及旅居申报表_20200221.docx", "temp", "健康及旅居申报表.docx", map, request, response);
+    }
+
+    @ApiOperation("导出多个健康及旅居申报表")
+    @GetMapping("/whepi/export/lyReports")
+    public @ResponseBody
+    void exportSellPlan2(HttpServletRequest request, HttpServletResponse response) {
+
+        List<Map<String, Object>> list = Lists.newArrayList();
+        for (int i = 0; i < 5; i++) {
+            Map<String, Object> map = new HashMap<String, Object>();
+            map.put("userName", "张山");
+            map.put("workPlace", "武汉");
+            map.put("telPhone", "13245452345");
+            map.put("isLeave", "是");
+            map.put("trip", "北京、上海、武汉");
+            map.put("backTime", "2020-03-10");
+            map.put("vehicle", "火箭");
+
+            Map<String, Object> rt = new HashMap<String, Object>();
+            map.put("rt", rt);
+            rt.put("d1", "02-20");
+            rt.put("d2", "02-21");
+            rt.put("d3", "02-22");
+            rt.put("d4", "02-23");
+            rt.put("d5", "02-24");
+            rt.put("t1", "-");
+            rt.put("t2", "37.0");
+            rt.put("t3", "36.6");
+            rt.put("t4", "36.6");
+            rt.put("t5", "36.8");
+
+            map.put("otherSymptom", "ee");
+            map.put("isTrue", "否");
+            list.add(map);
+        }
+
+
+        WordUtils.exportWord("word/健康及旅居申报表_20200221.docx", "temp", "健康及旅居申报表.docx", list, request, response);
+    }
+
+    @ApiOperation("导出单个隔离人员解除隔离审批表")
+    @GetMapping("/whepi/export/lyApprove")
+    public @ResponseBody
+    void lyApprove(HttpServletRequest request, HttpServletResponse response) {
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("userName", "张山");
+        map.put("gander", "男");
+        map.put("org", "研发部");
+        map.put("bAddr", "北京");
+        map.put("backDate", "2020-03-10");
+
+        Map<String, Object> rt = new HashMap<String, Object>();
+        map.put("rt", rt);
+        rt.put("d1", "02-20");
+        rt.put("d2", "02-21");
+        rt.put("d3", "02-22");
+        rt.put("d4", "02-23");
+        rt.put("d5", "02-24");
+        rt.put("d6", "02-25");
+        rt.put("d7", "02-26");
+        rt.put("d8", "02-27");
+        rt.put("d9", "02-28");
+        rt.put("d10", "02-29");
+        rt.put("d11", "03-01");
+        rt.put("d12", "03-02");
+        rt.put("d13", "03-03");
+        rt.put("d14", "03-04");
+        rt.put("d15", "03-05");
+        rt.put("t1", "-");
+        rt.put("t2", "37.0");
+        rt.put("t3", "36.6");
+        rt.put("t4", "36.6");
+        rt.put("t5", "36.8");
+        rt.put("t6", "-");
+        rt.put("t7", "37.0");
+        rt.put("t8", "36.6");
+        rt.put("t9", "36.6");
+        rt.put("t10", "36.8");
+        rt.put("t11", "-");
+        rt.put("t12", "37.0");
+        rt.put("t13", "36.6");
+        rt.put("t14", "36.6");
+        rt.put("t15", "36.8");
+
+        map.put("otherSymptom", "无");
+
+        WordUtils.exportWord("word/隔离人员解除隔离审批表_20200221.docx", "temp", "隔离人员解除隔离审批表.docx", map, request, response);
+    }
+
+    @ApiOperation("导出多个隔离人员解除隔离审批表")
+    @GetMapping("/whepi/export/lyApproves")
+    public @ResponseBody
+    void lyApproves(HttpServletRequest request, HttpServletResponse response) {
+
+        List<Map<String, Object>> list = Lists.newArrayList();
+        for (int i = 0; i < 5; i++) {
+            Map<String, Object> map = new HashMap<String, Object>();
+            map.put("userName", "张山");
+            map.put("gander", "男");
+            map.put("org", "研发部");
+            map.put("bAddr", "北京");
+            map.put("backDate", "2020-03-10");
+
+            Map<String, Object> rt = new HashMap<String, Object>();
+            map.put("rt", rt);
+            rt.put("d1", "02-20");
+            rt.put("d2", "02-21");
+            rt.put("d3", "02-22");
+            rt.put("d4", "02-23");
+            rt.put("d5", "02-24");
+            rt.put("d6", "02-25");
+            rt.put("d7", "02-26");
+            rt.put("d8", "02-27");
+            rt.put("d9", "02-28");
+            rt.put("d10", "02-29");
+            rt.put("d11", "03-01");
+            rt.put("d12", "03-02");
+            rt.put("d13", "03-03");
+            rt.put("d14", "03-04");
+            rt.put("d15", "03-05");
+            rt.put("t1", "-");
+            rt.put("t2", "37.0");
+            rt.put("t3", "36.6");
+            rt.put("t4", "36.6");
+            rt.put("t5", "36.8");
+            rt.put("t6", "-");
+            rt.put("t7", "37.0");
+            rt.put("t8", "36.6");
+            rt.put("t9", "36.6");
+            rt.put("t10", "36.8");
+            rt.put("t11", "-");
+            rt.put("t12", "37.0");
+            rt.put("t13", "36.6");
+            rt.put("t14", "36.6");
+            rt.put("t15", "36.8");
+
+            map.put("otherSymptom", "无");
+            list.add(map);
+        }
+
+        WordUtils.exportWord("word/隔离人员解除隔离审批表_20200221.docx", "temp", "隔离人员解除隔离审批表.docx", list, request, response);
+    }
+}

+ 124 - 0
whepi-web/src/main/java/com/bofeng/word/WordUtils.java

@@ -0,0 +1,124 @@
+package com.bofeng.word;
+
+
+import java.io.*;
+import java.net.URLEncoder;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import cn.afterturn.easypoi.word.WordExportUtil;
+import com.yvan.DateUtils;
+import freemarker.template.Configuration;
+import freemarker.template.Template;
+import freemarker.template.utility.DateUtil;
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
+import org.springframework.beans.factory.annotation.Value;
+
+public class WordUtils {
+
+    /**
+     * 导出word
+     * <p>第一步生成替换后的word文件,只支持docx</p>
+     * <p>第二步下载生成的文件</p>
+     * <p>第三步删除生成的临时文件</p>
+     * 模版变量中变量格式:{{foo}}
+     * @param templatePath word模板地址
+     * @param temDir 生成临时文件存放地址
+     * @param fileName 文件名
+     * @param params 替换的参数
+     * @param request HttpServletRequest
+     * @param response HttpServletResponse
+     */
+    public static void exportWord(String templatePath, String temDir, String fileName, Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
+//        Assert.notNull(templatePath,"模板路径不能为空");
+//        Assert.notNull(temDir,"临时文件路径不能为空");
+//        Assert.notNull(fileName,"导出文件名不能为空");
+//        Assert.isTrue(fileName.endsWith(".docx"),"word导出请使用docx格式");
+        if (!temDir.endsWith("/")){
+            temDir = temDir + File.separator;
+        }
+        File dir = new File(temDir);
+        if (!dir.exists()) {
+            dir.mkdirs();
+        }
+        try {
+            String userAgent = request.getHeader("user-agent").toLowerCase();
+            if (userAgent.contains("msie") || userAgent.contains("like gecko")) {
+                fileName = URLEncoder.encode(fileName, "UTF-8");
+            } else {
+                fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
+            }
+            XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);
+            String tmpPath = temDir + fileName;
+            FileOutputStream fos = new FileOutputStream(tmpPath);
+            doc.write(fos);
+            // 设置强制下载不打开
+            response.setContentType("application/force-download");
+            // 设置文件名
+            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
+            OutputStream out = response.getOutputStream();
+            doc.write(out);
+            out.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+//            delAllFile(temDir);//这一步看具体需求,要不要删
+        }
+
+    }
+
+    /**
+     * 导出word
+     * <p>第一步生成替换后的word文件,只支持docx</p>
+     * <p>第二步下载生成的文件</p>
+     * <p>第三步删除生成的临时文件</p>
+     * 模版变量中变量格式:{{foo}}
+     * @param templatePath word模板地址
+     * @param temDir 生成临时文件存放地址
+     * @param fileName 文件名
+     * @param paramList 替换的参数
+     * @param request HttpServletRequest
+     * @param response HttpServletResponse
+     */
+    public static void exportWord(String templatePath, String temDir, String fileName, List<Map<String, Object>> paramList, HttpServletRequest request, HttpServletResponse response) {
+//        Assert.notNull(templatePath,"模板路径不能为空");
+//        Assert.notNull(temDir,"临时文件路径不能为空");
+//        Assert.notNull(fileName,"导出文件名不能为空");
+//        Assert.isTrue(fileName.endsWith(".docx"),"word导出请使用docx格式");
+        if (!temDir.endsWith("/")){
+            temDir = temDir + File.separator;
+        }
+        File dir = new File(temDir);
+        if (!dir.exists()) {
+            dir.mkdirs();
+        }
+        try {
+            String userAgent = request.getHeader("user-agent").toLowerCase();
+            if (userAgent.contains("msie") || userAgent.contains("like gecko")) {
+                fileName = URLEncoder.encode(fileName, "UTF-8");
+            } else {
+                fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
+            }
+            XWPFDocument doc = WordExportUtil.exportWord07(templatePath, paramList);
+            String tmpPath = temDir + fileName;
+            FileOutputStream fos = new FileOutputStream(tmpPath);
+            doc.write(fos);
+            // 设置强制下载不打开
+            response.setContentType("application/force-download");
+            // 设置文件名
+            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
+            OutputStream out = response.getOutputStream();
+            doc.write(out);
+            out.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+//            delAllFile(temDir);//这一步看具体需求,要不要删
+        }
+
+    }
+}

二進制
whepi-web/src/main/resources/word/健康及旅居申报表_20200221.docx


二進制
whepi-web/src/main/resources/word/隔离人员解除隔离审批表_20200221.docx