拆分表

Contents
[ ]

在Aspose.Words文档对象模型中表示的表格由独立的行和单元格组成,因此可以轻松拆分表格。

要操作一个表将其拆分为两个表,我们只需要将一些行从原始表移动到新表。 为此,我们需要选择要拆分表的行。

我们可以通过以下简单的步骤从原始表中创建两个表:

  1. 创建表的克隆,而不克隆子项以保留移动的行并将其插入到原始表之后
  2. 从指定行开始,将所有后续行移动到此第二个表

下面的代码示例演示如何将表拆分为特定行上的两个表:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "Tables.docx");
Table firstTable = (Table) doc.getChild(NodeType.TABLE, 0, true);
// We will split the table at the third row (inclusive).
Row row = firstTable.getRows().get(2);
// Create a new container for the split table.
Table table = (Table) firstTable.deepClone(false);
// Insert the container after the original.
firstTable.getParentNode().insertAfter(table, firstTable);
// Add a buffer paragraph to ensure the tables stay apart.
firstTable.getParentNode().insertAfter(new Paragraph(doc), firstTable);
Row currentRow;
do
{
currentRow = firstTable.getLastRow();
table.prependChild(currentRow);
} while (currentRow != row);
doc.save(getArtifactsDir() + "WorkingWithTables.SplitTable.docx");