java利用POI替换word文档中的标签

java利用POI替换word文档中的标签

		<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.13</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.13</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.13</version>
        </dependency>
 public static void main(String[] args) {
        Text text = new Text();
        text.word();
    }
    
    public Object word(){
        Map<String, String> mp = new HashMap<String, String>();
        File f = new File("D:\\测试.docx");
        mp.put("fileName", "Tom");
        try {
            InputStream is = new FileInputStream("C:\\Users\\52347\\Desktop\\文件操作测试2.docx");
            XWPFDocument doc = new XWPFDocument(is);
            replaceInWord(mp,doc ,f);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return mp;
    }
    private void replaceInWord(Map<String, String> replacements, XWPFDocument doc, File outfile) throws IOException {
        long count1 = 0;
        List<XWPFParagraph> paragraphs = doc.getParagraphs();
        List<XWPFTable> tables = doc.getTables();
        count1 = replaceInParagraphs(replacements, paragraphs, false);
        //判断如果输出的路径文件不存在就创建一个
        if (!outfile.exists()) {
            outfile.createNewFile();
        }
        doc.write(new FileOutputStream(outfile));
        System.out.println("段落替换数量累计:" + count1);
    }

    private long replaceInParagraphs(Map<String, String> replacements, List<XWPFParagraph> paragraphs, boolean flag) {
        long count = 0;
        String pString = flag ? "表格" + "段落清晰:" : "段落:";
        for (XWPFParagraph paragraph : paragraphs) {
            System.out.println(pString + paragraph.getText());
            List<XWPFRun> runs = paragraph.getRuns();
            for (Map.Entry<String, String> replPair : replacements.entrySet()) {
                String oldText = replPair.getKey();
                String newText = replPair.getValue();
                // 获取文本段
                TextSegement tSegement = paragraph.searchText(oldText, new PositionInParagraph());
                if (tSegement != null) {
                    int beginRun = tSegement.getBeginRun();
                    int endRun = tSegement.getEndRun();
                    System.out.println(beginRun + " " + endRun);
                    count++;
                    if (beginRun == endRun) {
                        // whole search string is in one Run
                        XWPFRun run = runs.get(beginRun);
                        String runText = run.getText(0);
                        System.out.println("runText:" + runText);
                        String replaced = runText.replace(oldText, newText);
                        run.setText(replaced, 0);
                    } else {
                        // The search string spans over more than one Run
                        // Put the Strings together
                        StringBuilder b = new StringBuilder();
                        for (int runPos = beginRun; runPos <= endRun; runPos++) {
                            XWPFRun run = runs.get(runPos);
                            b.append(run.getText(0));
                        }
                        String connectedRuns = b.toString();
                        String replaced = connectedRuns.replace(oldText, newText);

                        // The first Run receives the replaced String of all
                        // connected Runs
                        XWPFRun partOne = runs.get(beginRun);
                        partOne.setText(replaced, 0);
                        // Removing the text in the other Runs.
                        for (int runPos = beginRun + 1; runPos <= endRun; runPos++) {
                            XWPFRun partNext = runs.get(runPos);
                            partNext.setText("", 0);
                        }
                    }
                }
            }
        }
        return count;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值