翻譯|使用教程|編輯:李顯亮|2019-09-18 10:40:24.700|閱讀 1161 次
概述:在Word文檔和Aspose.Words文檔對象模型中,沒有列的概念。按照設計,Microsoft Word中的表行完全獨立,基本屬性和操作僅包含在表的行和單元格中。本文將介紹如何在表格中插入和刪除列。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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 節點組成。列也沒有本機支持。
通過遍歷表的行的相同單元索引來對列實現此類操作,下面的代碼通過證明一個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;
}下面的示例顯示如何將空白列插入表中:
//獲取文檔中的第一個表. 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群已開通,各類資源及時分享,歡迎交流討論!(掃描下方二維碼加入群聊)

本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn