poi-tl 根据 模板 生成 word 文档

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();
}

4. 效果

效果

效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值