微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何用word文档中的段落替换表格? Apache POI [解决方案]

如何解决如何用word文档中的段落替换表格? Apache POI [解决方案]

这是一个用段落替换表格的函数。我们将首先找到表格位置,然后将其从那里删除,并在同一位置添加一个段落。

public XWPFDocument replaceTableWithText(XWPFDocument document){
    String replaceText = "This is the text which needs to replace a table";
    int tableNumberInDocument = 1;
    List<XWPFTable> tables = document.getTables();
    XWPFTable theTable = tables.get(tableNumberInDocument);
    int position = document.getPosOfTable(theTable);        
    document.removeBodyElement(position);
    XWPFParagraph paragraph = document.createParagraph();
    XWPFRun xr = paragraph.createRun();  
    xr.setBold(true);  
    xr.setText(replaceText);
    xr.addBreak();
    xr.setFontSize(12);
    xr.setFontFamily("Arial");
    document.setParagraph(paragraph,position);
    return document;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。