使用XMLCursor将表插入XWPFDocument时创建的空框

时间:2015-03-31 09:14:22

标签: apache-poi xwpf

当我使用XMLCursor将表插入XWPFDocument时,它会将表插入正确的位置,但它会在第一列下添加一个额外的框。该框连接到表上,因此看起来它是一个额外的表格单元格,但是当我在不使用XMLCursor的情况下插入表格时,表格是正确的,并且该框位于XMLCursor的位置。有没有办法删除该框,因为它看起来像一个额外的表格单元格。

    XWPFDocument part1Document = new XWPFDocument(part1Package);
    XmlCursor xmlCursor = part1Document.getDocument().getBody().getPArray(26).newCursor();

       //create first row
     XWPFTable tableOne = part1Document.createTable();

        XWPFTableRow tableOneRowOne = tableOne.getRow(0);
        tableOneRowOne.getCell(0).setText("Hello");
        tableOneRowOne.addNewTableCell().setText("World");

        XWPFTableRow tableOneRowTwo = tableOne.createRow();
        tableOneRowTwo.getCell(0).setText("This is");
        tableOneRowTwo.getCell(1).setText("a table");

        tableOne.getRow(1).getCell(0).setText("only text");


       XmlCursor c2 = tableOne.getCTTbl().newCursor();

       c2.moveXml(xmlCursor);

       c2.dispose();
        XWPFTable tables = part1Document.insertNewTbl(xmlCursor);
       xmlCursor.dispose();

空盒子出现在第26段的位置。任何帮助都会很棒。感谢。

1 个答案:

答案 0 :(得分:0)

您所做的是实际创建两个不同的表。第一个使用方法createTable(),然后添加所需的行和单元格。然后在表tableOne上的光标处创建一个方法为insertNewTbl(xmlCursor)的新表。该方法创建一个包含1行和1个单元格的新表,因此是空框。

我不能说为什么你的第一个表实际上被放置在正确的位置,可能是因为它与第二个表合并,它会转移到你的光标。

我建议你从头开始创建第二个表:

XWPFTable tableOne = part1Document.insertNewTbl(xmlCursor);
XWPFTableRow tableOneRowOne = tableOne.getRow(0);
tableOneRowOne.getCell(0).setText("Hello");
tableOneRowOne.addNewTableCell().setText("World");
相关问题