原創|其它|編輯:郝浩|2012-10-19 17:26:14.000|閱讀 6570 次
概述:有時在文檔中已經存在一個一個表格,但是如果想添加一個復制的表然后應用一些修改,最簡單的方法就是......
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在使用Aspose.Words時,有時在文檔中已經存在一個一個表格,但是如果想添加一個復制的表然后應用一些修改,最簡單的方法就是:
C#
Document doc = new Document(MyDir + "Table.SimpleTable.doc"); // Retrieve the first table in the document. Table table = (Table)doc.GetChild(NodeType.Table, 0, true); // Create a clone of the table. Table tableClone = (Table)table.Clone(true); // Insert the cloned table into the document after the original table.ParentNode.InsertAfter(tableClone, table); // Insert an empty paragraph between the two tables or else they will be combined into one // upon save. This has to do with document validation. table.ParentNode.InsertAfter(new Paragraph(doc), table); doc.Save(MyDir + "Table.CloneTableAndInsert Out.doc");
Visual Basic
Dim doc As New Document(MyDir & "Table.SimpleTable.doc") ' Retrieve the first table in the document. Dim table As Table = CType(doc.GetChild(NodeType.Table, 0, True), Table) ' Create a clone of the table. Dim tableClone As Table = CType(table.Clone(True), Table) ' Insert the cloned table into the document after the original table.ParentNode.InsertAfter(tableClone, table) ' Insert an empty paragraph between the two tables or else they will be combined into one ' upon save. This has to do with document validation. table.ParentNode.InsertAfter(New Paragraph(doc), table) doc.Save(MyDir & "Table.CloneTableAndInsert Out.doc")
如果新表包含不同的內容,你需要首先清除現有表中的內容。
C#
foreach (Cell cell in tableClone.GetChildNodes(NodeType.Cell, true)) cell.RemoveAllChildren();
Visual Basic
For Each cell As Cell In tableClone.GetChildNodes(NodeType.Cell, True) cell.RemoveAllChildren() Next cell
同樣的技術可以用來添加副本的一個現有的行到一個表中。
C#
Document doc = new Document(MyDir + "Table.SimpleTable.doc"); // Retrieve the first table in the document. Table table = (Table)doc.GetChild(NodeType.Table, 0, true); // Clone the last row in the table. Row clonedRow = (Row)table.LastRow.Clone(true); // Remove all content from the cloned row's cells. This makes the row ready for // new content to be inserted into. foreach (Cell cell in clonedRow.Cells) cell.RemoveAllChildren(); // Add the row to the end of the table. table.AppendChild(clonedRow); doc.Save(MyDir + "Table.AddCloneRowToTable Out.doc");
Visual Basic
Dim doc As New Document(MyDir & "Table.SimpleTable.doc") ' Retrieve the first table in the document. Dim table As Table = CType(doc.GetChild(NodeType.Table, 0, True), Table) ' Clone the last row in the table. Dim clonedRow As Row = CType(table.LastRow.Clone(True), Row) ' Remove all content from the cloned row's cells. This makes the row ready for ' new content to be inserted into. For Each cell As Cell In clonedRow.Cells cell.RemoveAllChildren() Next cell ' Add the row to the end of the table. table.AppendChild(clonedRow) doc.Save(MyDir & "Table.AddCloneRowToTable Out.doc")
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網