入门必备!快速学会用Aspose.Words在表格中插入和删除列!

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


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

  • 表中的每一行能够具备彻底不一样数量的单元格。
  • 垂直地,每行的单元格能够具备不一样的宽度。
  • 能够链接具备不一样行格式和单元格计数的表。

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

经过遍历表的行的相同单元索引来对列实现此类操做,下面的代码经过证实一个façade类来收集组成表的“列”的单元格,从而使这些操做更容易。下面的示例演示了一个用于处理表的列的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;    
}

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

//获取文档中的第一个表.    
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();
相关文章
相关标签/搜索