zoukankan      html  css  js  c++  java
  • springboot使用模板导出word文件

    1.在pom.xml文件添加依赖

     1             <!-- 导出word -->
     2         <dependency>
     3             <groupId>cn.afterturn</groupId>
     4             <artifactId>easypoi-base</artifactId>
     5             <version>3.0.3</version>
     6         </dependency>
     7         <dependency>
     8             <groupId>cn.afterturn</groupId>
     9             <artifactId>easypoi-web</artifactId>
    10             <version>3.0.3</version>
    11         </dependency>
    12         <dependency>
    13             <groupId>cn.afterturn</groupId>
    14             <artifactId>easypoi-annotation</artifactId>
    15             <version>3.0.3</version>
    16         </dependency>     

    2.封装导出word工具类

     1 import cn.afterturn.easypoi.word.WordExportUtil;
     2 import org.apache.poi.xwpf.usermodel.XWPFDocument;
     3 import org.springframework.util.Assert;
     4 
     5 import javax.servlet.http.HttpServletRequest;
     6 import javax.servlet.http.HttpServletResponse;
     7 import java.io.File;
     8 import java.io.FileOutputStream;
     9 import java.io.OutputStream;
    10 import java.net.URLEncoder;
    11 import java.util.Map;
    12 
    13 public class ExportWordUtils {
    14     /**
    15      * 导出word
    16      * <p>第一步生成替换后的word文件,只支持docx</p>
    17      * <p>第二步下载生成的文件</p>
    18      * <p>第三步删除生成的临时文件</p>
    19      * 模版变量中变量格式:{{foo}}
    20      *
    21      * @param templatePath word模板地址
    22      * @param temDir       生成临时文件存放地址
    23      * @param fileName     文件名
    24      * @param params       替换的参数
    25      * @param request      HttpServletRequest
    26      * @param response     HttpServletResponse
    27      */
    28     public static void exportWord(String templatePath, String temDir, String fileName, Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
    29         Assert.notNull(templatePath, "模板路径不能为空");
    30         Assert.notNull(temDir, "临时文件路径不能为空");
    31         Assert.notNull(fileName, "导出文件名不能为空");
    32         Assert.isTrue(fileName.endsWith(".docx"), "word导出请使用docx格式");
    33         if (!temDir.endsWith("/")) {
    34             temDir = temDir + File.separator;
    35         }
    36         File dir = new File(temDir);
    37         if (!dir.exists()) {
    38             dir.mkdirs();
    39         }
    40         try {
    41             String userAgent = request.getHeader("user-agent").toLowerCase();
    42             if (userAgent.contains("msie") || userAgent.contains("like gecko")) {
    43                 fileName = URLEncoder.encode(fileName, "UTF-8");
    44             } else {
    45                 fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
    46             }
    47             XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);
    48             String tmpPath = temDir + fileName;
    49             FileOutputStream fos = new FileOutputStream(tmpPath);
    50             doc.write(fos);
    51             // 设置强制下载不打开
    52             response.setContentType("application/force-download");
    53             // 设置文件名
    54             response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
    55             OutputStream out = response.getOutputStream();
    56             doc.write(out);
    57             out.close();
    58         } catch (Exception e) {
    59             e.printStackTrace();
    60         } finally {
    61             //这一步看具体需求,要不要删
    62             delFileWord(temDir, fileName);
    63         }
    64     }
    65 
    66     /**
    67      * 删除零时生成的文件
    68      */
    69     public static void delFileWord(String filePath, String fileName) {
    70         File file = new File(filePath + fileName);
    71         File file1 = new File(filePath);
    72         file.delete();
    73         file1.delete();
    74     }
    75 }
  • 相关阅读:
    [YTU]_2417 C语言习题 字符串长度
    最小生成树学习笔记
    后缀数组学习笔记
    网络流的几个小优化
    面向对象
    Manacher(马拉车)学习笔记
    EXKMP学习笔记QAQ
    GDOI DAY1游记
    GDOI--DAY2 游记
    caioj:1348: [NOIP普及组2012]质因数分解 C++
  • 原文地址:https://www.cnblogs.com/sunxun001/p/13157277.html
Copyright © 2011-2022 走看看