原創|使用教程|編輯:龔雪|2015-08-17 16:36:16.000|閱讀 2099 次
概述: Aspose.Words表的合并與拆分的使用教程
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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")
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自:慧都控件網