如何使用apache poi更新.doc中的字段?

时间:2015-01-13 11:56:04

标签: java apache-poi doc

我有替换.doc文件中的字符串的方法,如何修改它以用字符串值替换字段?

private static HWPFDocument replaceText(HWPFDocument doc, String findText, String replaceText){
        Range rangeDoc = doc.getRange();

        for (int i = 0; i < rangeDoc.numSections(); ++i ) {
            Section s = rangeDoc.getSection(i);
            for (int x = 0; x < s.numParagraphs(); x++) {
                Paragraph p = s.getParagraph(x);
                for (int z = 0; z < p.numCharacterRuns(); z++) {
                    CharacterRun run = p.getCharacterRun(z);
                    String text = run.text();
                    if(text.contains(findText)) {
                        run.replaceText(findText, replaceText);
                    }
                }
            }
        }
        return doc;
    }

0 个答案:

没有答案