在应用和操作Word时,经常需要把多个表格合并为一个表格或是把一个表格分割为多个单独的表格,Aspose Words为广大开发人员提供了该功能,让开发人员可以轻松地实现表格的合并与分割,具体可以参考下面的代码:
1.怎么组合两个表格为一个表格
// 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");
2.如何分割一个表格为多个指定行数的单独表格
// 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");