RowCollection

RowCollection class

提供对集合的类型访问Row节点.

要了解更多信息,请访问使用表格文档文章。

public class RowCollection : NodeCollection

特性

姓名描述
Count { get; }获取集合中的节点数。
Item { get; }检索Row在给定的索引处。 (2 indexers)

方法

姓名描述
Add(Node)在集合末尾添加一个节点。
Clear()从此集合和文档中删除所有节点。
Contains(Node)确定节点是否在集合中。
GetEnumerator()提供对节点集合的简单“foreach”样式迭代。
IndexOf(Node)返回指定节点的从零开始的索引。
Insert(int, Node)将节点插入到指定索引的集合中。
Remove(Node)从集合和文档中删除节点。
RemoveAt(int)从集合和文档中删除指定索引处的节点。
ToArray()将集合中的所有行复制到新的行数组中。 (2 methods)

例子

展示如何遍历文档中的所有表格并打印每个单元格的内容。

Document doc = new Document(MyDir + "Tables.docx");
TableCollection tables = doc.FirstSection.Body.Tables;

Assert.AreEqual(2, tables.ToArray().Length);

for (int i = 0; i < tables.Count; i++)
{
    Console.WriteLine($"Start of Table {i}");

    RowCollection rows = tables[i].Rows;

    // 我们可以在行集合上使用“ToArray”方法将其克隆到数组中。
    Assert.AreEqual(rows, rows.ToArray());
    Assert.AreNotSame(rows, rows.ToArray());

    for (int j = 0; j < rows.Count; j++)
    {
        Console.WriteLine($"\tStart of Row {j}");

        CellCollection cells = rows[j].Cells;

        // 我们可以在单元格集合上使用“ToArray”方法将其克隆到数组中。
        Assert.AreEqual(cells, cells.ToArray());
        Assert.AreNotSame(cells, cells.ToArray());

        for (int k = 0; k < cells.Count; k++)
        {
            string cellText = cells[k].ToString(SaveFormat.Text).Trim();
            Console.WriteLine($"\t\tContents of Cell:{k} = \"{cellText}\"");
        }

        Console.WriteLine($"\tEnd of Row {j}");
    }

    Console.WriteLine($"End of Table {i}\n");
}

也可以看看