CellCollection Class

Provides typed access to a collection of Cell nodes.

Inheritance Hierarchy

Namespace:  Aspose.Words.Tables
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.7.0

Syntax

public class CellCollection : NodeCollection

The CellCollection type exposes the following members.

Properties

  NameDescription
Public propertyCode exampleCount
Gets the number of nodes in the collection.
(Inherited from NodeCollection.)
Public propertyCode exampleItem
Retrieves a Cell at the given index.

Methods

  NameDescription
Public methodCode exampleAdd
Adds a node to the end of the collection.
(Inherited from NodeCollection.)
Public methodCode exampleClear
Removes all nodes from this collection and from the document.
(Inherited from NodeCollection.)
Public methodCode exampleContains
Determines whether a node is in the collection.
(Inherited from NodeCollection.)
Public methodEquals (Inherited from Object.)
Public methodGetEnumerator
Provides a simple "foreach" style iteration over the collection of nodes.
(Inherited from NodeCollection.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodCode exampleIndexOf
Returns the zero-based index of the specified node.
(Inherited from NodeCollection.)
Public methodCode exampleInsert
Inserts a node into the collection at the specified index.
(Inherited from NodeCollection.)
Public methodCode exampleRemove
Removes the node from the collection and from the document.
(Inherited from NodeCollection.)
Public methodCode exampleRemoveAt
Removes the node at the specified index from the collection and from the document.
(Inherited from NodeCollection.)
Public methodCode exampleToArray
Copies all cells from the collection to a new array of cells.
Public methodToString (Inherited from Object.)

Examples

Shows how to iterate through all tables in the document and display the content from each cell.
Document doc = new Document(MyDir + "Tables.docx");

// Here we get all tables from the Document node. You can do this for any other composite node
// which can contain block level nodes. For example you can retrieve tables from header or from a cell
// containing another table (nested tables)
TableCollection tables = doc.FirstSection.Body.Tables;

// We can make a new array to clone all of the tables in the collection
Assert.AreEqual(2, tables.ToArray().Length);

// Iterate through all tables in the document
for (int i = 0; i < tables.Count; i++)
{
    // Get the index of the table node as contained in the parent node of the table
    Console.WriteLine($"Start of Table {i}");

    RowCollection rows = tables[i].Rows;

    // RowCollections can be cloned into arrays
    Assert.AreEqual(rows, rows.ToArray());
    Assert.AreNotSame(rows, rows.ToArray());

    // Iterate through all rows in the table
    for (int j = 0; j < rows.Count; j++)
    {
        Console.WriteLine($"\tStart of Row {j}");

        CellCollection cells = rows[j].Cells;

        // RowCollections can also be cloned into arrays 
        Assert.AreEqual(cells, cells.ToArray());
        Assert.AreNotSame(cells, cells.ToArray());

        // Iterate through all cells in the row
        for (int k = 0; k < cells.Count; k++)
        {
            // Get the plain text content of this cell
            string cellText = cells[k].ToString(SaveFormat.Text).Trim();
            // Print the content of the cell
            Console.WriteLine($"\t\tContents of Cell:{k} = \"{cellText}\"");
        }

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

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

See Also