0

当我使用 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 段的位置。任何帮助都会很棒。谢谢。

4

1 回答 1

0

您所做的是实际上创建了两个不同的表。第一个带有 method 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");
于 2015-08-06T08:25:39.117 回答