Aspose.Words使用教程之表的合并与拆分
教程 合并 拆分 words Aspose 使用
2023-09-11 14:14:49 时间
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")
了解更多Aspose文档管理产品 Aspose技术交流群(761297826)
相关文章
- Iocomp控件教程之LinearGauge--线性刻度尺控件
- Word控件Spire.Doc 【段落处理】教程(十七):在 C#、VB.NET 中的 Word 中按样式名称获取段落
- Word控件Spire.Doc 【段落处理】教程(七):如如何通过在 C# 中附加 HTML 代码来设置 word 项目符号样式
- Word控件Spire.Doc 【邮件合并】教程(五): 在 C# 中为组创建邮件合并
- Word控件Spire.Doc 【邮件合并】教程(四):如何使用邮件合并合并图像值
- Word控件Spire.Doc 【文档操作】教程(五):在 C#、VB.NET 中合并、设置多个 Word 文档
- Word控件Spire.Doc 转换教程(四):如何将 Word 转换为文本
- Aspose.Words for .NET使用表格教程之水平和垂直合并表格中单元格
- 【软件安装】Python2023版安装详细教程(附安装包)
- 最详细最简单的教程,搭建一个高可用的harbor镜像仓库
- 《例说8051:单片机程序设计案例教程》——2-3 输出电路设计
- 『 迷你教程 』Python的Classes类应用在Keras方法详解
- Flutter 教程之 如何添加本地照片通过assets管理(教程含源码)
- Excel WPS自动化之如何将多行合并为多列(Python源码含教程)
- TortoiseSVN 合并操作简明教程
- iOS应用id,套装id,appid,BundleID申请教程
- CYQ.Data V5 从入门到放弃ORM系列:教程 - Log、SysLogs两个日志类使用
- 【bat】bat命令教程
- CAD教程:CAD软件帮助功能的相关介绍
- TL-WR941N路由器刷DD-WRT和OPENWRT教程及使用花生壳