cn.afterturn
easypoi-base
4.4.0
cn.afterturn
easypoi-web
4.4.0
cn.afterturn
easypoi-annotation
4.4.0
org.apache.poi
ooxml-schemas
1.4
easypoi的版本必须在4.3.0以上,否则在导出图片的时候,只会导出图片的内存地址,却不能显示出图片。
package com.state.grid.substation.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.afterturn.easypoi.word.WordExportUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.util.Assert;
public class WordUtil {
public static void exportWord(String templatePath, String temDir, String fileName, Map params, HttpServletRequest request, HttpServletResponse response) {
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();
} 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();
}
}
1.像这种:用两个花括号括起来的变量名,到时候会将变量名所指代的数据填充进去
2.像这种:需要将批量数据循环导出的表格以及图片
调用service和mapper没写,具体以实际需求为准
去数据库查表获取想要的信息----用一个集合将这些信息都装起来----将这些信息都插入到模板的指定位置当中去
这个肯定不能直接拿来运行,注意看我写的注释,理解代码逻辑,我写的很清楚,每一行代码干了什么我都有写。
/**
* 巡视报告的导出(循环图片版)
* @param substationId
* @param calendarId
* @param request
* @param response
* @return
*/
//请求路径是/taskResultToWord,请求方式是get请求
@RequestMapping(value = "/taskResultToWord", method = RequestMethod.GET)
//返回Result类型,传进来变电站id,日历id
public Result taskResultToWord(String substationId, String calendarId, HttpServletRequest request, HttpServletResponse response) throws Exception { // 分页查询
int code = 0;
String msg = "";
// 定义List集合list,泛型是TaskResultEquipment
List list = new ArrayList<>();
// List uplist = new ArrayList<>();
//定义Map集合resmap
// Map resmap = new HashMap<>();
try {
//token验证
Des3Util des = new Des3Util();
String token = request.getHeader("token");
String jsonUser = des.parseDES(token);
JSONObject jsonData = JSONObject.fromObject(jsonUser);
String userId = (String) jsonData.get("u");
String roleId = (String) jsonData.get("r");
boolean access = this.roleService.getAccess(roleId, "task/taskList");
if(access){
//定义Map集合map
Map map = new HashMap<>();
//将输入的设备id添加到map集合里面
// map.put("substationId", substationId);
//将输入的日历id添加到calendarId里面
map.put("calendarId", calendarId);
//返回表v_task_result_equipment中的数据条数
int count = this.taskService.taskResultEquipmentCount(map);
//将查询到的基本信息赋给taskProgress
TaskProgress taskProgress = this.taskService.taskResultInfo(map);
//添加字段startRow(开始行)的值为0
map.put("startRow", 0);
//添加字段endRow(结束行)的值为count,count的值就是表v_task_result_equipment中的数据条数
map.put("endRow", count);
//分页查询表v_task_result_equipment中的数据,起始行是0,结束行是count,就是一页查询所有数据,
list = this.taskService.taskResultEquipmentList(map);
System.out.println(list);
//路径
String path1 = ResourceUtils.getURL("classpath:").getPath();
String path2 = path1.substring(1);
String terminalType = System.getProperty("os.name");
String path = "";
if (Objects.equals(terminalType, "Linux")){
path = path1.replace("ROOT/WEB-INF/classes/", "").replace("ROOT\\WEB-INF\\classes\\", "");
}else {
path = path2.replace("ROOT/WEB-INF/classes/", "").replace("ROOT\\WEB-INF\\classes\\", "");
}
System.out.println("path!!!!!!" + path);
//PATH是文件导出路径resource/excel/
String PATH = path + config.getExportUrl();
//ImagePath是图片导入路径resource/cameraImage/
String ImagePath = path + config.getCameraImageUrl();
//导出表头信息
//定义一个Map集合params
Map params = new HashMap<>();
//模板文件的路径templatePath
String templatePath = "E:/template/patrolWord.docx"; //模板路径
//简单渲染文本
params.put("substationName", taskProgress.getSubstationName());
params.put("voltageLevel", taskProgress.getVoltageLevel());
params.put("viewStartTime",taskProgress.getViewStartTime());
params.put("patrolType",taskProgress.getPatrolType());
params.put("taskName",taskProgress.getTaskName());
params.put("className",taskProgress.getClassName());
params.put("totalNumber",taskProgress.getTotalNumber());
params.put("execresult1",taskProgress.getExecresult1());
params.put("execresult2",taskProgress.getExecresult2());
params.put("execresult0",taskProgress.getExecresult0());
params.put("execresult3",taskProgress.getExecresult3());
params.put("execStartTime",taskProgress.getExecStartTime());
params.put("execEndTime",taskProgress.getExecEndTime());
/*ImageEntity image = new ImageEntity();
image.setHeight(30);
image.setWidth(30);
image.setUrl("static/log.jpg"); //url路径
//image.setData(out.toByteArray()); //字节流读取
//设置读取图片方式(必须)
image.setType(ImageEntity.URL);
//表格外添加简单图片
params.put("img", image);*/
//声明了一个名为alarms的变量,它是一个List类型,其中每个元素都是一个Map类型的键值对集合。
List
1.固定信息部分:
2.循环表格和循环图片部分