文檔首頁(yè)>>Aspose.Words使用教程>>Aspose.Words使用教程之表的合并與拆分
Aspose.Words使用教程之表的合并與拆分
Aspose.Words文檔對(duì)象模型的表格由獨(dú)立行和單元格組成,那樣可以方便地實(shí)現(xiàn)加入或劃分表格。
為了可以操作表格來(lái)與另外表格進(jìn)行拆分與添加,我們只需要將一個(gè)表的行移動(dòng)到另一個(gè)表里面即可。
兩張表結(jié)合為一張表:
注意:第二張表的行被轉(zhuǎn)移到第一張表的末尾并且第二張表會(huì)被刪除。
代碼如下:
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")
拆分一張表為兩張獨(dú)立表:
注意:我們首先需要選擇一個(gè)在哪兒分割表的行。一旦我們知道這個(gè)地方,遵循這些簡(jiǎn)單的步驟我們可以從原始表創(chuàng)建兩張表:
1.創(chuàng)建一個(gè)復(fù)制的表,然后從原始表移動(dòng)行并且插入進(jìn)這張表。
2.從指定的行所有后續(xù)行移動(dòng)到第二張表。
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")