首页 > 解决方案 > 如何用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;
}

标签: javaspring-bootapache-poireportingxwpf

解决方案


推荐阅读