【问题标题】:replace a text in MS word Templete(Docx) using java使用java替换MS word Templete(Docx)中的文本
【发布时间】:2019-02-01 17:12:23
【问题描述】:

我正在尝试在 docx 中搜索一个字符串并使用 java apache poi 替换为其他一些文本,但它正在随机替换 在行中得到错误作为 arrayIndexoutofbound 异常

"声明命名空间 w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:ffData/w:name/@w:val")[0];

public class WordReplaceTextInFormFields {

private static void replaceFormFieldText(XWPFDocument document, String ffname, String text) {
    boolean foundformfield = false;
    for (XWPFParagraph paragraph : document.getParagraphs()) {
        for (XWPFRun run : paragraph.getRuns()) {
            XmlCursor cursor = run.getCTR().newCursor();
            cursor.selectPath(
                    "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:fldChar/@w:fldCharType");
            while (cursor.hasNextSelection()) {
                cursor.toNextSelection();
                XmlObject obj = cursor.getObject();
                if ("begin".equals(((SimpleValue) obj).getStringValue())) {
                    cursor.toParent();
                    obj = cursor.getObject();
                    obj = obj.selectPath(
                            "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:ffData/w:name/@w:val")[0];
                    if (ffname.equals(((SimpleValue) obj).getStringValue())) {
                        foundformfield = true;
                    } else {
                        foundformfield = false;
                    }
                } else if ("end".equals(((SimpleValue) obj).getStringValue())) {
                    if (foundformfield)
                        return;
                    foundformfield = false;
                }
            }
            if (foundformfield && run.getCTR().getTList().size() > 0) {
                run.getCTR().getTList().get(0).setStringValue(text);
                // System.out.println(run.getCTR());
            }
        }
    }
}

public static void main(String[] args) throws Exception {

    XWPFDocument document = new XWPFDocument(new FileInputStream("WordTemplate.docx"));

    replaceFormFieldText(document, "Text1", "Моя Компания");
    replaceFormFieldText(document, "Text2", "Аксель Джоачимович Рихтер");
    replaceFormFieldText(document, "Text3", "Доверенность");

    document.write(new FileOutputStream("WordReplaceTextInFormFields.docx"));
    document.close();
}
}

它遗漏了一些字符串,它没有替换整个文档..请提供示例代码帮助

【问题讨论】:

    标签: java ms-word apache-poi docx


    【解决方案1】:

    我在https://github.com/centic9/poi-mail-merge 的项目中做了类似的事情,它提供了基于 POI 的通用邮件合并功能。它使用了与 XmlBeans 稍有不同的功能,它替换文档的完整 XML 内容中的字符串,而不是单独替换每个段落。

        private static void appendBody(CTBody src, String append, boolean first) throws XmlException {
            XmlOptions optionsOuter = new XmlOptions();
            optionsOuter.setSaveOuter();
            String srcString = src.xmlText();
            String prefix = srcString.substring(0,srcString.indexOf(">")+1);
    
            final String mainPart;
            // exclude template itself in first appending
            if(first) {
                mainPart = "";
            } else {
                mainPart = srcString.substring(srcString.indexOf(">")+1,srcString.lastIndexOf("<"));
            }
    
            String suffix = srcString.substring( srcString.lastIndexOf("<") );
            String addPart = append.substring(append.indexOf(">") + 1, append.lastIndexOf("<"));
            CTBody makeBody = CTBody.Factory.parse(prefix+mainPart+addPart+suffix);
            src.set(makeBody);
        }
    }
    

    line 132 in MailMerge.java

    【讨论】:

      猜你喜欢
      • 2020-08-27
      • 2014-09-08
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 2020-04-10
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多