與列及行一起工作
為了對表格工作有更多控制權,學習如何操作欄位和列。
找到表格元素索引
欄位、列和單元格都透過存取選中的檔案節點來管理,且透過其索引。 尋找任意節點的索引涉及從父節點收集所有子節點,然後使用 IndexOf 方法在集合中找到該節點的索引。
在一個文件中找到一張表的索引
有時你可能需要對某份文件的特定表格進行更改。 為了做到這一點,你可以根據其索引來參考一張表格。
接下來這個範例代碼示明如何從文件中獲取一張桌子的索引:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
NodeCollection allTables = doc.GetChildNodes(NodeType.Table, true); | |
int tableIndex = allTables.IndexOf(table); |
在一個表格中查找一行的索引
此外,您可能需要在選取的表格中對特定一行進行更改。 為了做到這一點,你也可以依其索引來參考一列。
接下來的程式碼範例示範如何取得一表格中某一行的索引:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
int rowIndex = table.IndexOf(table.LastRow); |
找出列中一單元格之索引
最後,你可能需要對特定一細胞進行更改,而您也可以透過細胞索引來執行此動作。
以下程式碼示例顯示如何取得列中一格的索引:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
int cellIndex = row.IndexOf(row.Cells[4]); |
與列 {#work-with-columns} 共同工作
在 Aspose.Words Document Object Model (DOM),Table 節點由 Row 節點組成,然後是 Cell 節點。 因此,在 Document
物件模型的 Aspose.Words 中(如文字處理檔案),沒有欄位的概念。
“設計上,第 Microsoft Word 和 Aspose.Words 行是完全獨立的,而基本的特性與運算僅包含在表格中的行和細胞中。” 這會讓表格有了一些有趣的屬性:
- 每一行桌格數目可能完全不同 垂直方向上,每行各行的細胞可以有不同的寬度。
- 有可能將不同行格式和細胞數的表格結合起來
對於列的任何操作實際上都是捷徑,它們會以改變行細胞的方式來進行操作,使其看起來像是對列進行了應用。 也就是說,您可以透過簡單地迭代相同的表行細胞索引來執行列上的運算。
接下來的程式碼示例透過提供一個用於收集表中構成欄位的細胞的facade類別,簡化這些運算:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
/// <summary> | |
/// Represents a facade object for a column of a table in a Microsoft Word document. | |
/// </summary> | |
internal class Column | |
{ | |
private Column(Table table, int columnIndex) | |
{ | |
mTable = table ?? throw new ArgumentException("table"); | |
mColumnIndex = columnIndex; | |
} | |
/// <summary> | |
/// Returns a new column facade from the table and supplied zero-based index. | |
/// </summary> | |
public static Column FromIndex(Table table, int columnIndex) | |
{ | |
return new Column(table, columnIndex); | |
} | |
/// <summary> | |
/// Returns the cells which make up the column. | |
/// </summary> | |
public Cell[] Cells => GetColumnCells().ToArray(); | |
/// <summary> | |
/// Returns the index of the given cell in the column. | |
/// </summary> | |
public int IndexOf(Cell cell) | |
{ | |
return GetColumnCells().IndexOf(cell); | |
} | |
/// <summary> | |
/// Inserts a brand new column before this column into the table. | |
/// </summary> | |
public Column InsertColumnBefore() | |
{ | |
Cell[] columnCells = Cells; | |
if (columnCells.Length == 0) | |
throw new ArgumentException("Column must not be empty"); | |
// Create a clone of this column. | |
foreach (Cell cell in columnCells) | |
cell.ParentRow.InsertBefore(cell.Clone(false), cell); | |
// This is the new column. | |
Column column = new Column(columnCells[0].ParentRow.ParentTable, mColumnIndex); | |
// We want to make sure that the cells are all valid to work with (have at least one paragraph). | |
foreach (Cell cell in column.Cells) | |
cell.EnsureMinimum(); | |
// Increase the index which this column represents since there is now one extra column in front. | |
mColumnIndex++; | |
return column; | |
} | |
/// <summary> | |
/// Removes the column from the table. | |
/// </summary> | |
public void Remove() | |
{ | |
foreach (Cell cell in Cells) | |
cell.Remove(); | |
} | |
/// <summary> | |
/// Returns the text of the column. | |
/// </summary> | |
public string ToTxt() | |
{ | |
StringBuilder builder = new StringBuilder(); | |
foreach (Cell cell in Cells) | |
builder.Append(cell.ToString(SaveFormat.Text)); | |
return builder.ToString(); | |
} | |
/// <summary> | |
/// Provides an up-to-date collection of cells which make up the column represented by this facade. | |
/// </summary> | |
private List<Cell> GetColumnCells() | |
{ | |
List<Cell> columnCells = new List<Cell>(); | |
foreach (Row row in mTable.Rows) | |
{ | |
Cell cell = row.Cells[mColumnIndex]; | |
if (cell != null) | |
columnCells.Add(cell); | |
} | |
return columnCells; | |
} | |
private int mColumnIndex; | |
private readonly Table mTable; | |
} |
以下範例示範如何在資料表中插入空白欄位:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Tables.docx"); | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
Column column = Column.FromIndex(table, 0); | |
// Print the plain text of the column to the screen. | |
Console.WriteLine(column.ToTxt()); | |
// Create a new column to the left of this column. | |
// This is the same as using the "Insert Column Before" command in Microsoft Word. | |
Column newColumn = column.InsertColumnBefore(); | |
foreach (Cell cell in newColumn.Cells) | |
cell.FirstParagraph.AppendChild(new Run(doc, "Column Text " + newColumn.IndexOf(cell))); |
以下範例顯示如何從文件的表格中移除一列:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Tables.docx"); | |
Table table = (Table) doc.GetChild(NodeType.Table, 1, true); | |
Column column = Column.FromIndex(table, 2); | |
column.Remove(); |
將行指定為標題行
您可以選擇將表格的第一行以標題列的形式僅在首頁上重複,或者如果表格被分成多個部分,在每個頁面上重複。 在 Aspose.Words 中,你可以用 HeadingFormat 屬性讓每個頁面都重複頭行。
您也可以標記多個標題列,如果這些列位於表格開頭的連續位置。 要做到這一點,你必須將這些行應用的 HeadingFormat 屬性。
以下範例代碼示範如何建立包含於隨後頁面重複的標題列的表格:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.StartTable(); | |
builder.RowFormat.HeadingFormat = true; | |
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; | |
builder.CellFormat.Width = 100; | |
builder.InsertCell(); | |
builder.Writeln("Heading row 1"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.Writeln("Heading row 2"); | |
builder.EndRow(); | |
builder.CellFormat.Width = 50; | |
builder.ParagraphFormat.ClearFormatting(); | |
for (int i = 0; i < 50; i++) | |
{ | |
builder.InsertCell(); | |
builder.RowFormat.HeadingFormat = false; | |
builder.Write("Column 1 Text"); | |
builder.InsertCell(); | |
builder.Write("Column 2 Text"); | |
builder.EndRow(); | |
} | |
doc.Save(ArtifactsDir + "WorkingWithTables.RepeatRowsOnSubsequentPages.docx"); |
保持表和行不跨頁顯示
有時該表格內容不應該跨頁。 例如,如果標題在表格上方,該標題和表格應始終在一起在同一頁上,以保持正確的外觀。
有兩種不同的技術可以幫助你達到這個功能:
Allow row break across pages
,適用於表格列Keep with next
,適用於表格細胞中的段落
預設時,上述屬性是關閉的。
保持列不會跨頁
這涉及限制一列中細胞內的內容,不能跨越頁面。 在 Microsoft Word 中,這個選項可以在表格屬性下找到,稱為允許列跨過頁面 。 在 Aspose.Words 中,這是在 RowFormat 的物件中作為屬性 Row 的 RowFormat.AllowBreakAcrossPages 。
接下來的一個程式碼範例說明如何禁用表中每一列的行跨頁動作:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Table spanning two pages.docx"); | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
// Disable breaking across pages for all rows in the table. | |
foreach (Row row in table.Rows) | |
row.RowFormat.AllowBreakAcrossPages = false; | |
doc.Save(ArtifactsDir + "WorkingWithTables.RowFormatDisableBreakAcrossPages.docx"); |
保持表格不會跨頁
若要阻止表格跨越頁面,我們必須指定,希望表格中的內容保持在一起。
為了做到這點,Aspose.Words使用了方法,這讓使用者可以選擇一張表格並啟用 KeepWithNext參數來 true 每個桌格內的段落。 這個例外是表中最後一個段落,應該設定為 false。
以下程式碼範例示範了如何設定表格在同一個頁面中保持在一起:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Table spanning two pages.docx"); | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
// We need to enable KeepWithNext for every paragraph in the table to keep it from breaking across a page, | |
// except for the last paragraphs in the last row of the table. | |
foreach (Cell cell in table.GetChildNodes(NodeType.Cell, true)) | |
{ | |
cell.EnsureMinimum(); | |
foreach (Paragraph para in cell.Paragraphs) | |
if (!(cell.ParentRow.IsLastRow && para.IsEndOfCell)) | |
para.ParagraphFormat.KeepWithNext = true; | |
} | |
doc.Save(ArtifactsDir + "WorkingWithTables.KeepTableTogether.docx"); |