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

java 利用 poi-tl 导出 word

 导出模板

图片处理

details是要遍历的表格里得数据,图片支持多张
        List<InfViolationDetail> details = infViolationDetailService.selectList(search);


        // 输出的图片对象列表
        List<PictureRenderData> imgList = null;
        if (!CollectionUtils.isEmpty(details)) {
            for (int i = 1; i <= details.size(); i++) {
                details.get(i-1).setId(Long.valueOf(i));
                String violationImg = details.get(i-1).getViolationImg();
                // 输出的图片对象列表
                imgList = new ArrayList<>();
                if (StringUtil.isNotBlank(violationImg)) {
                    violationImg = ","+violationImg;
                    String []urlStrs = violationImg.split(",");
                    for (int j = 0; j < urlStrs.length; j++) {
                        String url = platformUrl+urlStrs[j];
                        PictureRenderData picItem = Pictures.ofUrl(url).size( 60, 50) .center().create();
                        imgList.add(picItem);
                    }
                }
                details.get(i-1).setImgList(imgList);
            }
        }

 

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

InputStream resourceAsStream = ResourceReader.class.getClassLoader().getResourceAsStream("导出模板.docx");
        Map<String, Object> datas = new HashMap<String, Object>() {
            {
                put("details", details);

                put("violationStartPosition",infViolation.getViolationStartPosition());
                put("violationTime",DateUtils.dateToc(infViolation.getViolationTime()));
                put("ext1",infViolation.getExt1());
                put("ext19",infViolation.getExt19());
                put("ext15",infViolation.getExt15());
                put("ext20",infViolation.getExt20());
                put("inspector",infViolation.getInspector());
                put("ext21",infViolation.getExt21());
                put("punishmentSituation",infViolation.getPunishmentSituation());
                put("ext27",infViolation.getExt27());
                put("ext28",infViolation.getExt28());
                put("sgEnterName",infViolation.getSgEnterName());
            }
        };

        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        Configure config = Configure.builder()
                .bind("details", policy).build();

        XWPFTemplate template = XWPFTemplate.compile(resourceAsStream,config).render(datas);
        Map<String, Object> map = new HashMap<>();
        map.put("template", template);
        map.put("fileName", infViolation.getViolationStartPosition());
        return map;

输出word

    protected void buildWordDocument(String fileName, XWPFTemplate document, HttpServletResponse response) throws Exception {

        response.reset();
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ";filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8"));
        OutputStream os = response.getOutputStream();
        document.write(os);
        os.close();
    }

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,如果您要使用poi-tl导出Word文档并合并单元格,可以按照以下步骤进行操作: 1. 首先,准备一个Word模板文件,其中包含需要合并单元格表格。可以使用MS Word或其他工具创建模板文件。在模板文件中,使用`${}`占位符来标识需要填充数据的位置,使用`#for`和`#end`标记来循环生成多行数据。示例模板文件如下: ``` 姓名\t性别\t年龄\t住址 #for(user in users) ${user.name}\t${user.gender}\t${user.age}\t${user.address} #end ``` 2. 在Java代码中,使用poi-tl的模板引擎来加载模板文件,并将数据绑定到模板中。在模板中,使用`#merge`标记来指定需要合并的单元格区域。示例代码如下: ```java // 加载模板文件 InputStream is = new FileInputStream("template.docx"); XWPFTemplate template = XWPFTemplate.compile(is).render(new HashMap<String, Object>() {{ // 绑定数据 List<User> users = Arrays.asList( new User("张三", "男", 20, "北京"), new User("李四", "女", 22, "上海"), new User("王五", "男", 25, "广州") ); put("users", users); }}); // 获取表格合并单元格 IBody body = template.getDocument().getBody(); List<XWPFTable> tables = body.getTables(); XWPFTable table = tables.get(0); // 假设第一个表格需要合并单元格 table.getRow(0).getCell(0).setText("姓名"); table.getRow(0).getCell(1).setText("性别"); table.getRow(0).getCell(2).setText("年龄"); table.getRow(0).getCell(3).setText("住址"); for (int i = 1; i <= 3; i++) { table.getRow(i).getCell(0).setText("#{" + "users[" + (i - 1) + "].name}"); table.getRow(i).getCell(1).setText("#{" + "users[" + (i - 1) + "].gender}"); table.getRow(i).getCell(2).setText("#{" + "users[" + (i - 1) + "].age}"); table.getRow(i).getCell(3).setText("#{" + "users[" + (i - 1) + "].address}"); } table.mergeCells(1, 1, 2, 2); // 合并单元格 // 导出文档 template.write(new FileOutputStream("output.docx")); template.close(); ``` 在模板中,使用`${}`占位符来引用数据对象的字段,使用`#merge`标记来指定需要合并的单元格区域。在Java代码中,使用`table.mergeCells()`方法来合并单元格,其中参数分别为起始行、起始列、结束行、结束列。在本例中,我们将第2行第2列到第3行第3列的单元格进行了合并。 希望这些信息能够对您有所帮助!如果您还有其他问题,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值