解决poi操作docx替换${}占位符不成功的过程

先贴个代码

 public static boolean replaceAndGenerateWord(String srcPath, String destPath, Map<String, String> map) throws IOException {
        String[] sp = srcPath.split("\\.");
        String[] dp = destPath.split("\\.");
        // 判断文件有无扩展名
        if (sp.length <= 0 || dp.length <= 0) {
            return false;
        }

        if (
                !sp[sp.length - 1].equalsIgnoreCase("docx")
                        &&
                        !(
                                sp[sp.length - 1].equalsIgnoreCase("doc")
                                        && dp[dp.length - 1].equalsIgnoreCase("doc")
                        )
                ) {
            return false;
        }


        // 比较文件扩展名
        if (sp[sp.length - 1].equalsIgnoreCase("docx")) {
            XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(srcPath));
            // 替换段落中的指定文字
            Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();
            while (itPara.hasNext()) {
                XWPFParagraph paragraph = itPara.next();
                List<XWPFRun> runs = paragraph.getRuns();
                for (XWPFRun run : runs) {
                    String oneparaString = run.getText(run.getTextPosition());
                    if (StringUtil.isBlank(oneparaString)){
                        continue;
                    }
                    for (Map.Entry<String, String> entry :
                            map.entrySet()) {
                        oneparaString = oneparaString.replace(entry.getKey(), entry.getValue());
                    }
                    run.setText(oneparaString, 0);
                }

            }

            // 替换表格中的指定文字
            Iterator<XWPFTable> itTable = document.getTablesIterator();
            while (itTable.hasNext()) {
                XWPFTable table = itTable.next();
                int rcount = table.getNumberOfRows();
                for (int i = 0; i < rcount; i++) {
                    XWPFTableRow row = table.getRow(i);
                    List<XWPFTableCell> cells = row.getTableCells();
                    for (XWPFTableCell cell : cells) {
                        String cellTextString = cell.getText();
                        for (Map.Entry<String, String> e : map.entrySet()) {
                            cellTextString = cellTextString.replace(e.getKey(), e.getValue());
                        }
                        cell.removeParagraph(0);
                        cell.setText(cellTextString);
                    }
                }
            }
            FileOutputStream outStream = new FileOutputStream(destPath);
            document.write(outStream);
            outStream.close();
            return true;
        }
        // doc只能生成doc,如果生成docx会出错
        if ((sp[sp.length - 1].equalsIgnoreCase("doc"))
                && (dp[dp.length - 1].equalsIgnoreCase("doc"))) {
            HWPFDocument document = new HWPFDocument(new FileInputStream(srcPath));
            Range range = document.getRange();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                range.replaceText(entry.getKey(), entry.getValue());
            }
            FileOutputStream outStream = new FileOutputStream(destPath);
            document.write(outStream);
            outStream.close();
            return true;
        }

        return false;

    }

1.在段落替换部分,最小单位是XWPFRun,XWPFRun是一个XML节点,包含了各种样式,属性和文字

2.从XWPFRun中取出文字进行替换,失败的原因就是:${company}被分割在了数个XWPFRun里面,替换失败,看图:




解决办法:修改节点

1.word打开原docx文件,取消各种拼写检查,另存为 xml文件

2.文本编辑器打开xml,找到要替换的属性占位符,处理成如下样子:


3.保存xml,用word打开xml,另存为,docx

4.愉快地跑代码吧!

  • 9
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值