分割表
Contents
[
Hide
]
Aspose.Words Document Object Model 中表示的表格由独立的行和单元格组成,因此可以轻松拆分表格。
要操作一个表以将其拆分为两个表,我们只需将原始表中的一些行移动到新表即可。为此,我们需要选择要分割表的行。
我们可以通过以下简单步骤从原始表创建两个表:
- 创建表的克隆而不克隆子表以保留移动的行并将它们插入到原始表之后
- 从指定行开始,将所有后续行移至第二个表
以下代码示例演示如何将一个表拆分为特定行上的两个表:
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-.NET.git. | |
Document doc = new Document(MyDir + "Tables.docx"); | |
Table firstTable = (Table) doc.GetChild(NodeType.Table, 0, true); | |
// We will split the table at the third row (inclusive). | |
Row row = firstTable.Rows[2]; | |
// Create a new container for the split table. | |
Table table = (Table) firstTable.Clone(false); | |
// Insert the container after the original. | |
firstTable.ParentNode.InsertAfter(table, firstTable); | |
// Add a buffer paragraph to ensure the tables stay apart. | |
firstTable.ParentNode.InsertAfter(new Paragraph(doc), firstTable); | |
Row currentRow; | |
do | |
{ | |
currentRow = firstTable.LastRow; | |
table.PrependChild(currentRow); | |
} while (currentRow != row); | |
doc.Save(ArtifactsDir + "WorkingWithTables.SplitTable.docx"); |