Apache POI:替换段落文本

发布于 2024-09-13 14:42:50 字数 1626 浏览 1 评论 0原文

我正在使用 Apache POI 从模板生成 docx 文件。似乎没有一种明显的方法来替换段落中的所有文本,并且文档也非常稀缺。现在,我可以通过循环遍历文档的段落,然后循环遍历每个段落的运行,然后循环遍历每个运行的文本来读取文档...这工作得很好,我可以替换运行中文本的内容,但是我的模板占位符(例如:<>)可能会分为多次运行,这使得匹配和替换变得非常复杂。有没有办法设置 XWPFParagraph 的内容?或者至少有一种方法可以删除段落中的所有运行并创建我自己的运行?

这是我到目前为止所拥有的:

    public static void main(String[] args) {

    InputStream fs = null;
    try {
        fs = new FileInputStream("C:\\sample1.docx");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    XWPFDocument doc = null;
    try {
        doc = new XWPFDocument(fs);
    } catch (IOException e) {
        e.printStackTrace();
    }

    for (int i = 0; i < doc.getParagraphs().length; i++) {
        XWPFParagraph paragraph = doc.getParagraphs()[i];
        paragraph.getCTP().getRArray().

        // This will output the paragraph's contents.
        System.out.println(paragraph.getParagraphText());

        for (int j = 0; j < paragraph.getCTP().getRArray().length; j++) {
            CTR run = paragraph.getCTP().getRArray()[j];

            for (int k = 0; k < run.getTArray().length; k++) {
                CTText text = run.getTArray()[k];

                // This will output the text contents
                System.out.println(text.getStringValue());

                // And this will set its contents
                text.setStringValue("Success!");
            }
        }
    }

    try {
        doc.write(new FileOutputStream("C:\\output.docx"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I am using Apache POI to generate docx files from a template. There doesn't seem to be an obvious way to replace all text in a paragraph and the documentation is pretty scarce. Right now I am able to read a document by looping through its paragraphs, then looping through each paragraph's runs, then looping through each run's text... This works pretty well and I can replace the contents of a text in a run, but my template placeholders (example: <>) may be split into several runs, which makes it really complicated to match and replace. Is there a way to set the contents of a XWPFParagraph? Or at least a way to zap all runs in a paragraph and create my own runs?

Here is what I have so far:

    public static void main(String[] args) {

    InputStream fs = null;
    try {
        fs = new FileInputStream("C:\\sample1.docx");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    XWPFDocument doc = null;
    try {
        doc = new XWPFDocument(fs);
    } catch (IOException e) {
        e.printStackTrace();
    }

    for (int i = 0; i < doc.getParagraphs().length; i++) {
        XWPFParagraph paragraph = doc.getParagraphs()[i];
        paragraph.getCTP().getRArray().

        // This will output the paragraph's contents.
        System.out.println(paragraph.getParagraphText());

        for (int j = 0; j < paragraph.getCTP().getRArray().length; j++) {
            CTR run = paragraph.getCTP().getRArray()[j];

            for (int k = 0; k < run.getTArray().length; k++) {
                CTText text = run.getTArray()[k];

                // This will output the text contents
                System.out.println(text.getStringValue());

                // And this will set its contents
                text.setStringValue("Success!");
            }
        }
    }

    try {
        doc.write(new FileOutputStream("C:\\output.docx"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文