12

我正在使用 apache poi 3.8 将值写入 word 模板。我用所需的值替换 word 文件(键)中的特定字符串,例如 word 文档有一个包含键 %Entry1% 的段落,我想用“Entry text line1 \nnew line”替换它。在我的实现中,所有替换的键和值都存储在 Map 中。

Map<String, String> replacedElementsMap;

HWPFDocument 的代码是:

Range range = document.getRange();
for(Map.Entry<String, String> entry : replacedElementsMap.entrySet()) {
            range.replaceText(entry.getKey(), entry.getValue());
}

此代码工作正常,我只需将 \n 放入输入字符串中即可换行。但是我找不到 XWPFDocument 的类似方法。我当前的 XWPFDocument 代码是:

List<XWPFParagraph> xwpfParagraphs = document.getParagraphs();
for(XWPFParagraph xwpfParagraph : xwpfParagraphs) {
            List<XWPFRun> xwpfRuns = xwpfParagraph.getRuns();
            for(XWPFRun xwpfRun : xwpfRuns) {
                String xwpfRunText = xwpfRun.getText(xwpfRun.getTextPosition());
                for(Map.Entry<String, String> entry : replacedElementsMap.entrySet()) {
                    if (xwpfRunText != null && xwpfRunText.contains(entry.getKey())) {
                        xwpfRunText = xwpfRunText.replaceAll(entry.getKey(), entry.getValue());
                    }
                }
                xwpfRun.setText(xwpfRunText, 0);
            }
        }

现在“\n”-字符串不会导致回车,如果我使用, xwpfRun.addCarriageReturn();我只会在段落后得到一个换行符。我应该如何在 xwpf 中正确创建新行?

4

2 回答 2

26

我有另一个解决方案,它更容易:

            if (data.contains("\n")) {
                String[] lines = data.split("\n");
                run.setText(lines[0], 0); // set first line into XWPFRun
                for(int i=1;i<lines.length;i++){
                    // add break and insert new text
                    run.addBreak();
                    run.setText(lines[i]);
                }
            } else {
                run.setText(data, 0);
            }
于 2014-07-29T06:19:18.047 回答
3

毕竟,我必须手动创建段落。基本上,我将替换字符串拆分为一个数组,并为每个数组元素创建一个新段落。这是代码:

protected void replaceElementInParagraphs(List<XWPFParagraph> xwpfParagraphs,
                                              Map<String, String> replacedMap) {
        if (!searchInParagraphs(xwpfParagraphs, replacedMap)) {
            replaceElementInParagraphs(xwpfParagraphs, replacedMap);
        }
    }

 private boolean searchInParagraphs(List<XWPFParagraph> xwpfParagraphs, Map<String, String> replacedMap) {
        for(XWPFParagraph xwpfParagraph : xwpfParagraphs) {
            List<XWPFRun> xwpfRuns = xwpfParagraph.getRuns();
            for(XWPFRun xwpfRun : xwpfRuns) {
                String xwpfRunText = xwpfRun.getText(xwpfRun.getTextPosition());
                for(Map.Entry<String, String> entry : replacedMap.entrySet()) {
                    if (xwpfRunText != null && xwpfRunText.contains(entry.getKey())) {
                        if (entry.getValue().contains("\n")) {
                            String[] paragraphs = entry.getValue().split("\n");
                            entry.setValue("");
                            createParagraphs(xwpfParagraph, paragraphs);
                            return false;
                        }
                        xwpfRunText = xwpfRunText.replaceAll(entry.getKey(), entry.getValue());
                    }
                }
                xwpfRun.setText(xwpfRunText, 0);
            }
        }
        return true;
    }

 private void createParagraphs(XWPFParagraph xwpfParagraph, String[] paragraphs) {
        if(xwpfParagraph!=null){
            for (int i = 0; i < paragraphs.length; i++) {
                XmlCursor cursor = xwpfParagraph.getCTP().newCursor();
                XWPFParagraph newParagraph = document.insertNewParagraph(cursor);
                newParagraph.setAlignment(xwpfParagraph.getAlignment());
                newParagraph.getCTP().insertNewR(0).insertNewT(0).setStringValue(paragraphs[i]);
                newParagraph.setNumID(xwpfParagraph.getNumID());
            }
            document.removeBodyElement(document.getPosOfParagraph(xwpfParagraph));
        }
    }
于 2013-02-13T06:40:46.210 回答