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");
}

也可以看看