【问题标题】:Apache POI replace text in docx with JavaApache POI 用 Ja​​va 替换 docx 中的文本
【发布时间】:2021-08-06 04:55:17
【问题描述】:

我可以替换表格和页脚内的文字,但我不能替换表格外的文字。我不知道为什么。

请知道如何替换表格外的${name} 之类的段落? 我想要在地图上。

public static boolean changWord(String inputUrl, String outputUrl, Map<String, String> textMap) {

        // Template conversion default success
        boolean changeFlag = true;
        try {
            File file = new File(outputUrl);
            FileOutputStream stream = new FileOutputStream(file);
            @SuppressWarnings("resource")
            XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));
            WorderToNewWordUtils.changeText(document, textMap);
           
            document.write(stream);
            stream.close();

        } catch (IOException e) {
            e.printStackTrace();
            changeFlag = false;
        }

        return changeFlag;

    }

    public static void changeText(XWPFDocument document, Map<String, String> textMap) {
        for (XWPFParagraph p : document.getParagraphs()) {
            for (XWPFRun r : p.getRuns()) {
               
                String text = r.getText(0);
                if (checkText(text)) {
                    r.setText(changeValue(r.toString(), textMap), 0);
                }
            }
        }

        // Replace Text inside Table
        for (XWPFTable tbl : document.getTables()) {
            for (XWPFTableRow row : tbl.getRows()) {
                for (XWPFTableCell cell : row.getTableCells()) {
                    for (XWPFParagraph p : cell.getParagraphs()) {
                        for (XWPFRun r : p.getRuns()) {
                            String text = r.getText(0);
                            if (checkText(text)) {
                                
                                r.setText(changeValue(r.toString(), textMap), 0);
                            }
                            // System.out.println("Bevor Fußzeiler" + text);
                        }
                    }
                }
            }
        }

        // Replace Text in Footer
        for (XWPFFooter footer : document.getFooterList()) {
            for (XWPFParagraph paragraph1 : footer.getParagraphs()) {
                for (XWPFRun r : paragraph1.getRuns()) {
                    String text = r.getText(0);
                    if (checkText(text)) {
                      
                        r.setText(changeValue(r.toString(), textMap), 0);
                    }
                    // System.out.println("Nach Fußzeile" + text);
                }
            }
        }
    }

    public static boolean checkText(String text) {
        boolean check = false;
        if (text.indexOf("$") != -1) {
            check = true;
        }
        return check;
    }
    public static String changeValue(String value, Map<String, String> textMap) {
        for (Map.Entry<String, String> textSet : textMap.entrySet()) {
            // match template and replacement value format ${key}
            String key = "${" + textSet.getKey() + "}";
           
            if (value.indexOf(key) != -1) {
                value = textSet.getValue();

            }
        }
      
        return value;

    }
    public static void main(String[] args) {
        // Template file address
        String inputUrl = "D:\\Test.docx";

        Map<String, String> testMap = new HashMap<>();

        testMap.put("ja", "Nein");
        testMap.put("red", "Blue");
        testMap.put("No", "yes");
        testMap.put("Preis", "999$");
        testMap.put("Something", "Nothing");
        testMap.put("nein", "Ja");
        testMap.put("antwort", "Schöne");       
        testMap.put("name", "Sayer");
        testMap.put("Test", "Email");
        // .pdf if you want the Document in PDF Format
        String outputUrl = "D:\\New-Test.docx";

        WorderToNewWordUtils.changWord(inputUrl, outputUrl, testMap);

    }
}

【问题讨论】:

  • 您永远无法确定${name} 是否在Word 中的一个文本运行中。如果拼写检查处于活动状态,那么将至少有三个文本运行${name}。尝试使用apache poi 4 或更高版本的TextSegment。见stackoverflow.com/questions/65275097/…
  • 感谢您的帮助,我观看了示例,但这不是我想要的。我想告诉我的 HashMap 查找表(XWPFTable)、页脚(XWPFFooter)和段落(XWPFParagraph)。调试器告诉我,他只能找到 Tabels 变量。我希望你能理解我。

标签: java maven replace apache-poi docx


【解决方案1】:

我找到了一个解决方案,如果你想把你的电子邮件发给我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    相关资源
    最近更新 更多