使用Apache POI替换excel xlsx文件中的文本

时间:2016-08-09 08:40:47

标签: java apache apache-poi

我刚刚发现Apache POI库对于使用Java编辑Word文件非常有用。现在我想用一些其他文本替换我的XLSX文件中的一些文本(标记)。 是否有一些功能可以覆盖xlsx文档? 用word文件替换其他文本的示例:

static void replaceTag(String tagName, String value) throws Exception {
    for (XWPFParagraph p : doc.getParagraphs()) {
        List<XWPFRun> runs = p.getRuns();
        if (runs != null) {
            for (XWPFRun r : runs) {
                String text = r.getText(0);
                if (text != null && text.contains(tagName)) {
                    text = text.replace(tagName, value);
                    r.setText(text, 0);
                }
            }
        }
    }
   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.contains(tagName)) {
                            text = text.replace(tagName, value);
                            r.setText(text);
                        }
                    }
                }
            }
        }
    }
}

提前致谢:)

0 个答案:

没有答案
相关问题