🤞EasyPOI实现Word多页导出:按照指定模板根据List数据导出多页Word🤞 先看案例 模板:
导出后:导出了5张word
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.4.0</version>
</dependency>
package com.ruoyi.common.utils.poi;
import cn.afterturn.easypoi.word.WordExportUtil;
import cn.afterturn.easypoi.word.parse.ParseWord07;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
/**
* @author: YinLei
* Package: com.ruoyi.common.utils.poi
* @date: 2024/1/29 14:44
* @Description: word工具类
* @version: 1.0
*/
public class WordUtil<T> {
/**
* word下载
*
* @param fileName 下载时的文件名称
* @param response
* @param doc
*/
private static void downLoadWord(String fileName, HttpServletResponse response, XWPFDocument doc) throws IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/msword");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".docx" , "UTF-8"));
doc.write(response.getOutputStream());
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
/**
* word模板导出
* @param map
* @param templatePath
* @param fileName
* @param response
* @throws Exception
*/
public static void WordTemplateExport(Map<String, Object> map, String templatePath, String fileName, HttpServletResponse response) throws Exception {
XWPFDocument doc = WordExportUtil.exportWord07(templatePath, map);
downLoadWord(fileName,response,doc);
}
/**
* word模板导出多页
* @param list
* @param templatePath
* @param fileName
* @param response
* @throws Exception
*/
public static void WordTemplateExportMorePage(List<Map<String, Object>> list, String templatePath, String fileName, HttpServletResponse response) throws Exception {
XWPFDocument doc = new ParseWord07().parseWord(templatePath, list);
downLoadWord(fileName, response, doc);
}
}
@ApiOperation("word测试")
@GetMapping("/word")
public void makeWordTemplateMorePage(HttpServletResponse response) {
List<Map<String, Object>> list=new ArrayList<>();
for (int i = 0; i < 5; i++) {
Map<String, Object> person = new HashMap<>();
person.put("name", "张三"+i);
list.add(person);
}
try {
WordUtil.WordTemplateExportMorePage(list, "E:/test.docx", "文件测试名", response);
} catch (Exception e) {
e.printStackTrace();
}
}
大功告成,撒花致谢🎆🎇🌟,关注我不迷路,带你起飞带你富。