Aspose.Words for .NET使用表格教程之在表格中插入和删除列

Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

接下来我们将进入“使用格式”的介绍,其中包括应用格式、介绍和创建表、添加和拆分表以及使用列和行。

>>Aspose.Words for .NET更新至最新版v19.9,欢迎下载体验


在Word文档和Aspose.Words文档对象模型中,没有列的概念。按照设计,Microsoft Word中的表行完全独立,基本属性和操作仅包含在表的行和单元格中。这为表提供了一些有趣属性的可能性:

  • 表中的每一行可以具有完全不同数量的单元格。
  • 垂直地,每行的单元格可以具有不同的宽度。
  • 可以连接具有不同行格式和单元格计数的表。

对Microsoft Word中的列执行的任何操作实际上都是“快捷方法”,它通过共同修改行的单元格来执行操作,使得它们看起来应用于列。在Aspose.Words文档对象模型中, Table 节点由 Row 和 Cell 节点组成。列也没有本机支持。

通过遍历表的行的相同单元索引来对列实现此类操作,下面的代码通过证明一个fade类来收集组成表的“列”的单元格,从而使这些操作更容易。下面的示例演示了一个用于处理表的列的Facade对象。

///     ///表示Microsoft Word文档中表的列的Facade对象。   ///     internal class Column    {    private Column(Table table, int columnIndex)    {    if (table == null)    throw new ArgumentException("table");    mTable = table;    mColumnIndex = columnIndex;    }    ///     /// 从表中返回一个新的列Facade,并提供从零开始的索引。    ///     public static Column FromIndex(Table table, int columnIndex)    {    return new Column(table, columnIndex);    }    ///     /// 返回组成列的单元格。///     public Cell[] Cells    {    get    {    return (Cell[])GetColumnCells().ToArray(typeof(Cell));    }    }    ///     ///返回列中给定单元格的索引。    ///     public int IndexOf(Cell cell)    {    return GetColumnCells().IndexOf(cell);    }    ///     ///在此列之前插入一个全新的列到表中。///     public Column InsertColumnBefore()    {    Cell[] columnCells = Cells;    if (columnCells.Length == 0)    throw new ArgumentException("Column must not be empty");    //创建此列的克隆。    foreach (Cell cell in columnCells)    cell.ParentRow.InsertBefore(cell.Clone(false), cell);    //这是新专栏.    Column column = new Column(columnCells[0].ParentRow.ParentTable, mColumnIndex);    //我们希望确保单元格都可以使用(至少有一个段落)。foreach (Cell cell in column.Cells)    cell.EnsureMinimum();    // 增加此列表示的索引,因为现在有一个额外的列前面。  mColumnIndex++;    return column;    }    ///     ///从表中删除列。   ///     public void Remove()    {    foreach (Cell cell in Cells)    cell.Remove();    }    ///     /// 返回列的文本。   ///     public string ToTxt()    {    StringBuilder builder = new StringBuilder();    foreach (Cell cell in Cells)    builder.Append(cell.ToString(SaveFormat.Text));    return builder.ToString();    }    ///     ///提供构成此外观所代表的列的最新单元格集合。///     private ArrayList GetColumnCells()    {    ArrayList columnCells = new ArrayList();    foreach (Row row in mTable.Rows)    {    Cell cell = row.Cells[mColumnIndex];    if (cell != null)    columnCells.Add(cell);    }    return columnCells;    }    private int mColumnIndex;    private Table mTable;    }

下面的示例显示如何将空白列插入表中:

//获取文档中的第一个表.    Table table = (Table)doc.GetChild(NodeType.Table, 0, true);    // 获取表格中的第二列.    Column column = Column.FromIndex(table, 0);    //将列的纯文本打印到屏幕.    Console.WriteLine(column.ToTxt());    //在此列的左侧创建一个新列.    //这与在Microsoft Word中使用“Insert Column Before”命令相同.    Column newColumn = column.InsertColumnBefore();    //为每个列单元格添加一些文本.    foreach (Cell cell in newColumn.Cells)    cell.FirstParagraph.AppendChild(new Run(doc, "Column Text " + newColumn.IndexOf(cell)));

下面的示例演示如何从文档中的表中删除列:

//获取文档中的第二个表.Table table = (Table)doc.GetChild(NodeType.Table, 1, true);//从表中获取第三列并将其删除.Column column = Column.FromIndex(table, 2);column.Remove();

*悦满中秋 · 购享好礼,现在购买Aspose系列产品即可领取精美礼品哟,更多活动详情咨询在线客服了解哦~


ASPOSE技术交流QQ群已开通,各类资源及时分享,欢迎交流讨论!(扫描下方二维码加入群聊)

1560231367164.png

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年8月15日
下一篇 2019年8月15日

相关推荐

发表回复

登录后才能评论