使用poi根据word模板生成word

7 篇文章 0 订阅

需求:根据固定的word模板进行数据填充生成word
具体代码:

 try {
 			//获取文件操作对象 inputStream word模板的输入流
            XWPFDocument document = new XWPFDocument(inputStream);
            //获取段落迭代器
            Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();
            String text;
            Set<String> set;
            XWPFParagraph paragraph;
            List<XWPFRun> run;
            String key;
            nextTime:
            //判断是否还有段落
            while (itPara.hasNext()) {
                //获取段落元素
                paragraph = itPara.next();
                //这个map中 我设置的是对象要取的属性值和要替换的word模板的字符
                set = map.keySet();
                //获取列集合
                run = paragraph.getRuns();
                if (run.size() < 1) {
                    continue nextTime;
                }
                Iterator<String> iterator = set.iterator();
                while (iterator.hasNext()) {
                    key = iterator.next();
                    //循环列的值
                    for (int i = 0, runSie = run.size(); i < runSie; i++) {
                        XWPFRun xwpfRun = run.get(i);
                        //得到当前列的元素文本
                        text = xwpfRun.getText(xwpfRun.getTextPosition());
                        int i1 = text.indexOf(key);
                        //如果当前元素文本是要替换的字符
                        if (text != null && i1 > -1) {
                              //进行文本替换
                            xwpfRun.setText(text.replace(key, String.valueOf(map.get(key))), 0);
                        }
                    }
                }
                XWPFRun xwpfRun = run.get(0);
                String erWeiMaIndexText = xwpfRun.getText(xwpfRun.getTextPosition());
                if (erWeiMaIndexText != null && erWeiMaIndexText.equals("ERWEIMA")) {
                    java.io.File file1 = QRcodeGenerator.from(ServletUtils.getFullContextPath(request) + "/service/rest/tk.File/" + fileId + "/view").file();
                    FileInputStream fileInputStream = new FileInputStream(file1);
                    BufferedImage read = ImageIO.read(file1);
                    //设置图片,fileInputStream图片输入流,类型,名称,宽,高
                    xwpfRun.addPicture(fileInputStream, Document.PICTURE_TYPE_PNG, file1.getName(), Units.pixelToEMU(100), Units.pixelToEMU(100));
                    xwpfRun.setText(" ", 0);
                }
                if (erWeiMaIndexText != null && erWeiMaIndexText.equals("CURRENTTIME")) {
                    xwpfRun.setText(DateUtil.formatDate(new Date(), "yyyy年MM月dd日"), 0);
                }
            }
            //写入文件outputStream
            document.write(outputStream);
        } finally {
        //关闭流
            outputStream.close();
            inputStream.close();
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值