Aspose.Words文檔對象模型的表格由獨立行和單元格組成,那樣可以方便地實現加入或劃分表格。
爲了可以操作表格來與另外表格進行拆分與添加,我們只需要將一個表的行移動到另一個表裏面即可。
兩張表結合爲一張表:
注意:第二張表的行被轉移到第一張表的末尾並且第二張表會被刪除。
代碼如下:
C#
// Load the document. Document doc = new Document(MyDir + "Table.Document.doc"); // Get the first and second table in the document. // The rows from the second table will be appended to the end of the first table. Table firstTable = (Table)doc.GetChild(NodeType.Table, 0, true); Table secondTable = (Table)doc.GetChild(NodeType.Table, 1, true); // Append all rows from the current table to the next. // Due to the design of tables even tables with different cell count and widths can be joined into one table. while (secondTable.HasChildNodes) firstTable.Rows.Add(secondTable.FirstRow); // Remove the empty table container. secondTable.Remove(); doc.Save(MyDir + "Table.CombineTables Out.doc");
Visual Basic
' Load the document. Dim doc As New Document(MyDir & "Table.Document.doc") ' Get the first and second table in the document. ' The rows from the second table will be appended to the end of the first table. Dim firstTable As Table = CType(doc.GetChild(NodeType.Table, 0, True), Table) Dim secondTable As Table = CType(doc.GetChild(NodeType.Table, 1, True), Table) ' Append all rows from the current table to the next. ' Due to the design of tables even tables with different cell count and widths can be joined into one table. Do While secondTable.HasChildNodes firstTable.Rows.Add(secondTable.FirstRow) Loop ' Remove the empty table container. secondTable.Remove() doc.Save(MyDir & "Table.CombineTables Out.doc")
拆分一張表爲兩張獨立表:
注意:我們首先需要選擇一個在哪兒分割表的行。一旦我們知道這個地方,遵循這些簡單的步驟我們可以從原始表創建兩張表:
1.創建一個複製的表,然後從原始表移動行並且插入進這張表。
2.從指定的行所有後續行移動到第二張表。
C#
// Load the document. Document doc = new Document(MyDir + "Table.SimpleTable.doc"); // Get the first table in the document. 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(MyDir + "Table.SplitTable Out.doc");
Visual Basic
' Load the document. Dim doc As New Document(MyDir & "Table.SimpleTable.doc") ' Get the first table in the document. Dim firstTable As Table = CType(doc.GetChild(NodeType.Table, 0, True), Table) ' We will split the table at the third row (inclusive). Dim row As Row = firstTable.Rows(2) ' Create a new container for the split table. Dim table As Table = CType(firstTable.Clone(False), Table) ' 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) Dim currentRow As Row Do currentRow = firstTable.LastRow table.PrependChild(currentRow) Loop While currentRow IsNot row doc.Save(MyDir & "Table.SplitTable Out.doc")