【问题标题】:How replace String in the XWPFTableCell of docx如何替换docx的XWPFTableCell中的String
【发布时间】:2018-09-16 04:20:41
【问题描述】:

我正在使用 apache-poi 读取 docx 文件。在 wordfile 中,我需要替换每个等于 ${key} 的键。当这个键在段落中时,我可以,但是我的键在 docx 表中,因此,我需要在这个键的位置迭代数组值,例如:

enter image description here

遵循元素树

enter image description here

知道我是如何与该键下方的值进行交互的。

【问题讨论】:

  • 将您的代码添加到您的问题中,以便我们帮助找到解决方案:)

标签: apache-poi


【解决方案1】:

例如,我在文档中有一个段落单元格,它有 2 个字段,RG:和 CPF:我的第二行完成但我的第一行没有

while (tcell.getParagraphs().size() > 1) {
    tcell.removeParagraph(1);
}
XWPFParagraph p = tcell.getParagraphs().get(0);
if (p.getRuns().size() > 0) {
    p.removeRun(0);
    String[] lines = value.split("\n");
    p.createRun().setText(lines[0]);
    for (int i = 1; i < lines.length; i++) {
        p.getRuns().get(0).addBreak();  
        p.getRuns().get(0).setText(lines[i]);
    }
} else { 
    tcell.setText(value);
}

enter image description here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2023-02-09
    • 2013-04-08
    • 1970-01-01
    相关资源
    最近更新 更多