1.导入maven包
<dependency> <groupId>org.jeecg</groupId> <artifactId>easypoi-base</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.jeecg</groupId> <artifactId>easypoi-web</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.jeecg</groupId> <artifactId>easypoi-annotation</artifactId> <version>2.3.1</version> </dependency>
2。创建word文档样式
创建word文件夹将word放入到文件夹中
3.创建导出工具类
package cn.net.hylink.qingzhi.utils;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.jeecgframework.poi.word.WordExportUtil;
import org.springframework.util.Assert;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
public class ExportWordUtils {
public static void exportWord(String templatePath, String temDir, String fileName, Map<String, Object> params,HttpServletResponse response,HttpServletRequest request) {
/* 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();
// XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);
// String tmpPath = temDir + fileName;
// FileOutputStream fos = new FileOutputStream(tmpPath);
// doc.write(fos);
// fos.flush();
// // 设置强制下载不打开
//
// // 设置文件名
//
// //doc.write(out);
// //out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
//delFileWord(temDir,fileName);//这一步看具体需求,要不要删
}
}
/**
* 删除零时生成的文件
*/
public static void delFileWord(String filePath, String fileName){
File file =new File(filePath+fileName);
File file1 =new File(filePath);
file.delete();
file1.delete();
}
}
4.controller创建方法
@RequestMapping(value = "/ceshi", method = RequestMethod.GET)
public void ceshi() throws ParseException {
Map<String, Object> params = new HashMap<>();
params.put("rwdj","三级");
params.put("createtime", "2021-01-15 08:03");
ExportWordUtils.exportWord("word/export.docx", "D:/test", "ceshi.docx", params, response, request);
}
map里的key对应word里的变量值 就可以直接将值赋值到word里.
表格里可以填写list的值 如下:
在map里添加伟list的值就可以循环绑定上.
在赋值上样式问题的代码:
根据自己的需要变换word里的变量值