Apache的POI:替换段落文本 [英] Apache POI: Replace paragraph text

查看:2128
本文介绍了Apache的POI:替换段落文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

下面是我到目前为止有:

 公共静态无效的主要(字串[] args){    InputStream的FS = NULL;
    尝试{
        FS =新的FileInputStream(C:\\\\ sample1.docx);
    }赶上(FileNotFoundException异常五){
        e.printStackTrace();
    }
    XWPFDocument DOC = NULL;
    尝试{
        DOC =新XWPFDocument(FS);
    }赶上(IOException异常五){
        e.printStackTrace();
    }    的for(int i = 0; I< doc.getParagraphs()长;我++){
        XWPFParagraph款= doc.getParagraphs()[我]
        paragraph.getCTP()。getRArray()。        //这会输出段落的内容。
        的System.out.println(paragraph.getParagraphText());        对于(INT J = 0; J< paragraph.getCTP()getRArray()长; J ++){
            CTR运行= paragraph.getCTP()getRArray()[J]。            对于(INT K = 0; K< run.getTArray()长,说明k ++){
                CTText文本= run.getTArray()[K];                //这会输出文本内容
                的System.out.println(text.getStringValue());                //这将设置其内容
                text.setStringValue(成功!);
            }
        }
    }    尝试{
        doc.write(新的FileOutputStream(C:\\\\ output.docx));
    }赶上(FileNotFoundException异常五){
        e.printStackTrace();
    }赶上(IOException异常五){
        e.printStackTrace();
    }
}


解决方案

我得到了它的3.7 Beta2中工作。该解决方案是不理想的,是一个有点令人费解,但它的工作对我的用例。现在有一个XWPFDocument#setParagraph方法,将这样的伎俩...我有一对夫妇对我的博客有POI写Word文档的例子 - tkgospodinov.com:

<一个href=\"http://tkgospodinov.com/writing-microsoft-word-documents-in-java-with-apache-poi/\">http://tkgospodinov.com/writing-microsoft-word-documents-in-java-with-apache-poi/

希望有所帮助。

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();
    }
}

解决方案

I got it working with 3.7-beta2. The solution is not ideal and is a little convoluted, but it worked for my use case. There is now a XWPFDocument#setParagraph method that will do the trick... I have a couple of examples of writing Word documents with POI on my blog - tkgospodinov.com:

http://tkgospodinov.com/writing-microsoft-word-documents-in-java-with-apache-poi/

Hope that helps.

这篇关于Apache的POI:替换段落文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆