当前位置:   article > 正文

poi-tl导出word(图片、表格(合并单元格、嵌套图片)、文字)_looprowtablerenderpolicy 合并单元格

looprowtablerenderpolicy 合并单元格

java 利用 poi-tl 导出 word

 导出模板

图片处理

details是要遍历的表格里得数据,图片支持多张
  1. List<InfViolationDetail> details = infViolationDetailService.selectList(search);
  2. // 输出的图片对象列表
  3. List<PictureRenderData> imgList = null;
  4. if (!CollectionUtils.isEmpty(details)) {
  5. for (int i = 1; i <= details.size(); i++) {
  6. details.get(i-1).setId(Long.valueOf(i));
  7. String violationImg = details.get(i-1).getViolationImg();
  8. // 输出的图片对象列表
  9. imgList = new ArrayList<>();
  10. if (StringUtil.isNotBlank(violationImg)) {
  11. violationImg = ","+violationImg;
  12. String []urlStrs = violationImg.split(",");
  13. for (int j = 0; j < urlStrs.length; j++) {
  14. String url = platformUrl+urlStrs[j];
  15. PictureRenderData picItem = Pictures.ofUrl(url).size( 60, 50) .center().create();
  16. imgList.add(picItem);
  17. }
  18. }
  19. details.get(i-1).setImgList(imgList);
  20. }
  21. }

 

 获取模板,绑定模板里得内容

  1. InputStream resourceAsStream = ResourceReader.class.getClassLoader().getResourceAsStream("导出模板.docx");
  2. Map<String, Object> datas = new HashMap<String, Object>() {
  3. {
  4. put("details", details);
  5. put("violationStartPosition",infViolation.getViolationStartPosition());
  6. put("violationTime",DateUtils.dateToc(infViolation.getViolationTime()));
  7. put("ext1",infViolation.getExt1());
  8. put("ext19",infViolation.getExt19());
  9. put("ext15",infViolation.getExt15());
  10. put("ext20",infViolation.getExt20());
  11. put("inspector",infViolation.getInspector());
  12. put("ext21",infViolation.getExt21());
  13. put("punishmentSituation",infViolation.getPunishmentSituation());
  14. put("ext27",infViolation.getExt27());
  15. put("ext28",infViolation.getExt28());
  16. put("sgEnterName",infViolation.getSgEnterName());
  17. }
  18. };
  19. LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
  20. Configure config = Configure.builder()
  21. .bind("details", policy).build();
  22. XWPFTemplate template = XWPFTemplate.compile(resourceAsStream,config).render(datas);
  23. Map<String, Object> map = new HashMap<>();
  24. map.put("template", template);
  25. map.put("fileName", infViolation.getViolationStartPosition());
  26. return map;

输出word

  1. protected void buildWordDocument(String fileName, XWPFTemplate document, HttpServletResponse response) throws Exception {
  2. response.reset();
  3. response.setContentType("application/octet-stream");
  4. response.setHeader("Content-disposition", "attachment;filename="+ fileName + ";filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8"));
  5. OutputStream os = response.getOutputStream();
  6. document.write(os);
  7. os.close();
  8. }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号