01.导入依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
02. 工具类
/**
* @Author: SwordsMan 133-8660-9751
* @Date: 2020/4/10 14:30
* @Describe:
*/
public class WordUtils {
/**
* 生成word
*
* @param path
* @param params
* @param tableList
* @param fileName
* @param response
* @throws Exception
*/
public void getWord(String path, Map<String, Object> params, List<List<String[]>> tableList, String fileName, HttpServletResponse response) throws Exception {
URL url = WordUtils.class.getClassLoader().getResource(path);
InputStream is = new FileInputStream(WordUtils.getResource(path));
CustomXWPFDocument doc = new CustomXWPFDocument(is);
this.replaceInPara(doc, params);//替换文本里面的变量
for (int i = 0; i < tableList.size(); i++) {//2 个
this.replaceInTable(doc, params, i, tableList.get(i));
}
//this.replaceInTable(doc, params, tableList); //替换表格里面的变量
OutputStream os = response.getOutputStream();
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
doc.write(os);
this.close(os);
this.close(is);
}
/**
* 获取路径
*
* @param relativePath
* @return
* @throws IOException
*/
public static String getResource(String relativePath) throws IOException {
String filePath = System.getProperty("user.dir")