提问者:小点点

使用ApachePOI编辑Word文档[关闭]


想要改进这个问题?更新问题,使其仅通过编辑这篇文章专注于一个问题。

我正在尝试阅读word文档模板,然后用用户给定的数据替换模板中的变量,而不改变模板上的标题或样式。我不确定我正在做的事情是否正确,但这是我开始的方式:

'XWPFDocument docx = new XWPFDocument(
            new FileInputStream(
                    "D://TestDocumentPrep/src/XXXXX_TestReport_URL_Document.docx"));
    XWPFWordExtractor we = new XWPFWordExtractor(docx);
    String textData = we.getText();
    String newTestData=textData.replace("$var_source_code$", list.get(1))
            .replace("$var_rsvp_code$", list.get(2))
            .replace("$var_ssn$", list.get(3))
            .replace("$var_zip_code$", list.get(4))
            .replace("$var_point_for_business$",
                    anotherData.getPointForBusiness())
            .replace("$var_E1_url$", anotherData.getE1url())
            .replace("$var_E2_url$", anotherData.getE2url())
            .replace("$var_E3_url$", anotherData.getE3url());
    System.out.println(newTestData);'

这就是我所做的。但是它将word文档的内容作为字符串读取并替换variables.Now如何将替换的字符串以模板格式放入word文档中?

在这里我找到了一些东西,但不完全是我的解决方案

在这里我也找到了一些东西,但不完全是解决方案


共1个答案

匿名用户

嗨我能找到解决办法

这是我用来编辑我的word文档的代码,它对.文档和. docx格式的文件都很好,我想在不改变基本模板的情况下编辑和生成一个编辑过的新word文档。

public void wordDocProcessor(AnotherVO anotherData, ArrayList<String> list,
        String sourse, String destination) throws IOException,
        InvalidFormatException {
    XWPFDocument doc = new XWPFDocument(OPCPackage.open(sourse
            + "XXXXX_TestReport_URL_Document.doc"));
    for (XWPFTable tbl : doc.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 (text != null
                                && text.contains("var_source_code")) {
                            text = text.replace("var_source_code",
                                    list.get(1));
                            r.setText(text, 0);
                        }
                        if (text != null && text.contains("var_rsvp_code")) {
                            text = text.replace("var_rsvp_code",
                                    list.get(2));
                            r.setText(text, 0);
                        }
                        if (text != null && text.contains("var_ssn")) {
                            text = text.replace("var_ssn", list.get(3));
                            r.setText(text, 0);
                        }
                        if (text != null && text.contains("var_zip_code")) {
                            text = text
                                    .replace("var_zip_code", list.get(4));
                            r.setText(text, 0);
                        }
                        if (text != null
                                && text.contains("var_point_for_business")) {
                            text = text.replace("var_point_for_business",
                                    anotherData.getPointForBusiness());
                            r.setText(text, 0);
                        }
                        if (text != null && text.contains("var_E1_url")) {
                            text = text.replace("var_E1_url",
                                    anotherData.getE1url());
                            r.setText(text, 0);
                        }
                        if (text != null && text.contains("var_E2_url")) {
                            text = text.replace("var_E2_url",
                                    anotherData.getE2url());
                            r.setText(text, 0);
                        }
                        if (text != null && text.contains("var_E3_url")) {
                            text = text.replace("var_E3_url",
                                    anotherData.getE3url());
                            r.setText(text, 0);
                        }
                    }
                }
            }
        }
    }
    doc.write(new FileOutputStream(destination + list.get(0)
            + "_TestReport_URL_Document.doc"));
}