1.项目信息, 2.两张项目图片总览, 3.每个表格数据跟一张图片
poi-tl模板, 点击下载, 根据代码, 可以直接使用的,代码记得改路径
1.依赖
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
2.模板样式
3. java代码
bind(“detailList”, policy), 不太理解64行的bind, 欢迎交流, 效果是实现了
public void exportDataWordTest(HttpServletResponse response) throws Exception {
//开始位置2张图片
List<String> urls = new ArrayList<>();
urls.add("D:\\test\\6789123.jpg");
urls.add("D:\\test\\7891234.jpg");
List<Map<String, Object>> urlList= new ArrayList<>();
for (int i = 0; i < urls.size(); i++) {
HashMap<String, Object> url = new HashMap<>();
//本地图片
url.put("urlImg", Pictures.ofLocal(urls.get(i)).size(100, 100).create());
urlList.add(url);
}
HashMap<String, Object> data = new HashMap<>();
List<Map<String, Object>> tList= new ArrayList<>();
for (int j = 0; j < 2; j++){
List<Map<String,Object>> detailList=new ArrayList<>();
for (int i = 0; i < 2; i++) {
Map<String,Object> detailMap = new HashMap<>();
detailMap.put("index", i+1);//序号
detailMap.put("tdytqlx", "tdytqlx"+i);
detailMap.put("blockCount", "blockCount"+i);
detailMap.put("area", "area"+i);
detailMap.put("checkResults", "checkResults"+i);
detailList.add(detailMap);
}
Map<String, Object> tMap = new HashMap<>();
tMap.put("layerName", "layerName"+j);
tMap.put("detailList", detailList);
tMap.put("urlImg", Pictures.ofLocal("D:\\test\\123.jpg").size(100, 100).create());
tList.add(tMap);
}
data.put("urlListLoop",urlList);
data.put("typeProducts", tList);
data.put("XMMC", "XMMC");
data.put("XMBH", "XMBH");
data.put("XMLB", "XMLB");
data.put("YDDW", "YDDW");
data.put("YDMJ", "YDMJ");
data.put("JSNX", "JSNX");
data.put("ZYX", "ZYX");
String resource = "D:/template/" + "template1.docx";//word模板地址
//渲染表格 动态行
HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
Configure config = Configure.builder().bind("detailList", policy).build();//.bind("localImg", policy)
XWPFTemplate template = XWPFTemplate.compile(resource, config).render(data);
//=================生成文件保存在本地D盘某目录下=================
String temDir = "D:/template/";
//生成临时文件存放地址
//生成文件名
Long time = new Date().getTime();
// 生成的word格式
String formatSuffix = ".docx";
// 拼接后的文件名
String fileName = "xmmc" + "-" + time + formatSuffix;//文件名 带后缀
String name = temDir + fileName;
FileOutputStream fos = new FileOutputStream(name);
template.write(fos);
//=================生成word到设置浏览默认下载地址=================
// 设置强制下载不打开
response.setContentType("application/force-download");
// 设置文件名
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
OutputStream out = response.getOutputStream();
template.write(out);
out.flush();
out.close();
template.close();
}