Aspose. The table of the Words Document Object model is made up of separate rows and cells, which makes it easy to join or divide tables.
In order to be able to manipulate the table to split and add to another table, we just need to move the row of one table into the other.
Two tables are combined into a single table :
Note: The row of the second table is shifted to the end of the first table and the second table is deleted.
The code is as follows:
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 ();d OC. 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")
Split a table into two separate tables:
Note: We first need to select a row where the table is divided. Once we know this place, following these simple steps we can create two tables from the original table:
1. Create a replicated table, then move the row from the original table and insert it into the table .
2. Move all subsequent rows from the specified row to the second table.
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 rowDo currentrow = firsttable.lastrow table. PrependChild (CurrentRow) Loop while currentrow isnot rowdoc. Save (mydir & "Table.splittable out.doc")
aspose.
Words latest version download
Aspose.words merging and splitting using the tutorial table