【问题标题】:Empty box created when inserting a table using XMLCursor to XWPFDocument使用 XMLCursor 将表插入 XWPFDocument 时创建的空框
【发布时间】:2015-08-06 16:25:39
【问题描述】:

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

【问题讨论】:

    标签: apache-poi xwpf


    【解决方案1】:

    您实际上是创建了两个不同的表。第一个使用方法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");
    

    【讨论】:

      猜你喜欢
      • 2010-09-10
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多