拆分表
Contents
[
Hide
]
在Aspose.Words文档对象模型中表示的表格由独立的行和单元格组成,因此可以轻松拆分表格。
要操作一个表将其拆分为两个表,我们只需要将一些行从原始表移动到新表。 为此,我们需要选择要拆分表的行。
我们可以通过以下简单的步骤从原始表中创建两个表:
- 创建表的克隆,而不克隆子项以保留移动的行并将其插入到原始表之后
- 从指定行开始,将所有后续行移动到此第二个表
下面的代码示例演示如何将表拆分为特定行上的两个表:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto doc = MakeObject<Document>(MyDir + u"Tables.docx"); | |
auto firstTable = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true)); | |
// We will split the table at the third row (inclusive). | |
SharedPtr<Row> row = firstTable->get_Rows()->idx_get(2); | |
// Create a new container for the split table. | |
auto table = System::ExplicitCast<Table>(firstTable->Clone(false)); | |
// Insert the container after the original. | |
firstTable->get_ParentNode()->InsertAfter(table, firstTable); | |
// Add a buffer paragraph to ensure the tables stay apart. | |
firstTable->get_ParentNode()->InsertAfter(MakeObject<Paragraph>(doc), firstTable); | |
SharedPtr<Row> currentRow; | |
do | |
{ | |
currentRow = firstTable->get_LastRow(); | |
table->PrependChild(currentRow); | |
} while (currentRow != row); | |
doc->Save(ArtifactsDir + u"WorkingWithTables.SplitTable.docx"); |