删除XWPFTableRow的边框

时间:2016-03-09 09:06:07

标签: java apache-poi

我有一个带边框的XWPFTable,我在里面添加了一个分页符:

addBreak(BreakType.PAGE)

在下一页中,我想删除最后一个XWPFTableRow的边框,但我不能,因为此行不提供对边框的访问权限。边框属于表格。

如何删除最后一行的边框?

3 个答案:

答案 0 :(得分:1)

以下代码显示了如何访问/添加CTTcBorders对象以根据需要设置标志。

    XWPFDocument doc = new XWPFDocument();
    CTTbl ctTable = CTTbl.Factory.newInstance();
    XWPFTable table = new XWPFTable(ctTable, doc);
    XWPFTableRow tr = table.getRow(0);
    XWPFTableCell cell = tr.getCell(0);

    CTTc ctTc = cell.getCTTc();
    CTTcPr tcPr = ctTc.addNewTcPr();
    CTHMerge hMerge = tcPr.addNewHMerge();
    hMerge.setVal(STMerge.RESTART);

    CTTcBorders tblBorders = tcPr.addNewTcBorders();

对于现有文档,您需要遍历对象以找到您想要调整的对象。

答案 1 :(得分:0)

解决方案是:

cell.getCTTc().getTcPr().getTcBorders().addNewRight().setVal(STBorder.NIL);

谢谢!

答案 2 :(得分:0)

我试图仅删除某些行的中间边框,而不仅仅是删除右边的边框。

我必须迭代所需的单元格,然后对于第 i 个单元格(cell = row.get(i)),删除 i-th的右边框单元格 AND 下一个单元格的左边框(nextCell = row.get(i + 1))。

一个单元格的右边界与该行中下一个单元格的左边界重合,我需要同时隐藏两者。

public void hideTableInnerBorders(XWPFTable table){
    // for the 5th and 6th rows (index 4,5),
    // hide the right border for the three middle columns (index 1,2,3)
    // and the left border on the three last columns (index 2,3,4)
    List<XWPFTableRow> rows = table.getRows();
    for( int rowIndex = 4; rowIndex < rows.size(); rowIndex++){ //  rowIndex:{4,5} - last two lines
        List<XWPFTableCell> cells = rows.get(rowIndex).getTableCells();
        for(int cellIndex = 1; cellIndex < cells.size(); cellIndex++){ //  cellIndex:{1,2,3,4}
            XWPFTableCell cell = cells.get(cellIndex);
            CTTcBorders tblBorders = cell.getCTTc().getTcPr().addNewTcBorders();

            // remove the right border for the indexes 1 2 3
            if( cellIndex == 1 || cellIndex == 2 || cellIndex == 3 ){
                tblBorders.addNewRight().setVal(STBorder.NIL);
            }
            // remove the left border for the indexes 2,3,4
            if ( cellIndex == 2 || cellIndex == 3 || cellIndex == 4 ) {
                tblBorders.addNewLeft().setVal(STBorder.NIL);
            }
        }
    }
}

结果是: enter image description here

相关问题