springboot使⽤模板导出word⽂件
1.在l⽂件添加依赖
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⼯具类
1import cn.afterturn.easypoi.word.WordExportUtil;
2import org.apache.poi.xwpf.usermodel.XWPFDocument;
3import org.springframework.util.Assert;
4
5import javax.servlet.http.HttpServletRequest;
6import javax.servlet.http.HttpServletResponse;
7import java.io.File;
8import java.io.FileOutputStream;
9import java.io.OutputStream;
10import java.URLEncoder;
11import java.util.Map;
12
13public 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*/
28public static void exportWord(String templatePath, String temDir, String fileName, Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
29        Null(templatePath, "模板路径不能为空");
30        Null(temDir, "临时⽂件路径不能为空");
31        Null(fileName, "导出⽂件名不能为空");
32        Assert.dsWith(".docx"), "word导出请使⽤docx格式");
33if (!dsWith("/")) {
34            temDir = temDir + File.separator;
35        }
36        File dir = new File(temDir);springframework依赖
37if (!ists()) {
38            dir.mkdirs();
39        }
40try {
41            String userAgent = Header("user-agent").toLowerCase();
42if (ains("msie") || ains("like gecko")) {
43                fileName = de(fileName, "UTF-8");
44            } else {
45                fileName = new Bytes("utf-8"), "ISO-8859-1");
46            }
47            XWPFDocument doc = portWord07(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 = OutputStream();
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*/
69public 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 }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。