Cell Class

Represents a table cell.
Inheritance Hierarchy

Namespace:  Aspose.Words.Tables
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.2
Syntax
public class Cell : CompositeNode

The Cell type exposes the following members.

Constructors
  NameDescription
Public methodCode exampleCell
Initializes a new instance of the Cell class.
Properties
  NameDescription
Public propertyCode exampleCellFormat
Provides access to the formatting properties of the cell.
Public propertyCode exampleChildNodes
Gets all immediate child nodes of this node.
(Inherited from CompositeNode.)
Public propertyCode exampleCount
Gets the number of immediate children of this node.
(Inherited from CompositeNode.)
Public propertyCode exampleDocument
Gets the document to which this node belongs.
(Inherited from Node.)
Public propertyCode exampleFirstChild
Gets the first child of the node.
(Inherited from CompositeNode.)
Public propertyCode exampleFirstParagraph
Gets the first paragraph among the immediate children.
Public propertyCode exampleHasChildNodes
Returns true if this node has any child nodes.
(Inherited from CompositeNode.)
Public propertyCode exampleIsComposite
Returns true as this node can have child nodes.
(Inherited from CompositeNode.)
Public propertyCode exampleIsFirstCell
True if this is the first cell inside a row; false otherwise.
Public propertyCode exampleIsLastCell
True if this is the last cell inside a row; false otherwise.
Public propertyCode exampleLastChild
Gets the last child of the node.
(Inherited from CompositeNode.)
Public propertyCode exampleLastParagraph
Gets the last paragraph among the immediate children.
Public propertyCode exampleNextSibling
Gets the node immediately following this node.
(Inherited from Node.)
Public propertyCode exampleNodeType
Returns NodeType.Cell.
(Overrides NodeNodeType.)
Public propertyCode exampleParagraphs
Gets a collection of paragraphs that are immediate children of the cell.
Public propertyCode exampleParentNode
Gets the immediate parent of this node.
(Inherited from Node.)
Public propertyCode exampleParentRow
Returns the parent row of the cell.
Public propertyCode examplePreviousSibling
Gets the node immediately preceding this node.
(Inherited from Node.)
Public propertyCode exampleRange
Returns a Range object that represents the portion of a document that is contained in this node.
(Inherited from Node.)
Public propertyCode exampleTables
Gets a collection of tables that are immediate children of the cell.
Methods
  NameDescription
Public methodCode exampleAccept
Accepts a visitor.
(Overrides NodeAccept(DocumentVisitor).)
Protected methodAcceptChildren (Inherited from CompositeNode.)
Protected methodAcceptCore (Inherited from CompositeNode.)
Public methodCode exampleAppendChild
Adds the specified node to the end of the list of child nodes for this node.
(Inherited from CompositeNode.)
Public methodCode exampleClone (Inherited from Node.)
Protected methodCoreRemoveSelfOnly (Inherited from CompositeNode.)
Public methodCode exampleEnsureMinimum
If the last child is not a paragraph, creates and appends one empty paragraph.
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodCode exampleGetAncestor(Type)
Gets the first ancestor of the specified object type.
(Inherited from Node.)
Public methodCode exampleGetAncestor(NodeType)
Gets the first ancestor of the specified NodeType.
(Inherited from Node.)
Public methodCode exampleGetChild
Returns an Nth child node that matches the specified type.
(Inherited from CompositeNode.)
Public methodCode exampleGetChildNodes
Returns a live collection of child nodes that match the specified type.
(Inherited from CompositeNode.)
Public methodCode exampleGetEnumerator
Provides support for the for each style iteration over the child nodes of this node.
(Inherited from CompositeNode.)
Public methodGetHashCode (Inherited from Object.)
Public methodCode exampleGetText
Gets the text of this node and of all its children.
(Inherited from CompositeNode.)
Public methodGetType (Inherited from Object.)
Public methodCode exampleIndexOf
Returns the index of the specified child node in the child node array.
(Inherited from CompositeNode.)
Public methodCode exampleInsertAfter
Inserts the specified node immediately after the specified reference node.
(Inherited from CompositeNode.)
Public methodCode exampleInsertBefore
Inserts the specified node immediately before the specified reference node.
(Inherited from CompositeNode.)
Protected methodMemberwiseClone (Inherited from Object.)
Public methodCode exampleNextPreOrder
Gets next node according to the pre-order tree traversal algorithm.
(Inherited from Node.)
Public methodCode examplePrependChild
Adds the specified node to the beginning of the list of child nodes for this node.
(Inherited from CompositeNode.)
Public methodCode examplePreviousPreOrder
Gets the previous node according to the pre-order tree traversal algorithm.
(Inherited from Node.)
Public methodCode exampleRemove
Removes itself from the parent.
(Inherited from Node.)
Public methodCode exampleRemoveAllChildren
Removes all the child nodes of the current node.
(Inherited from CompositeNode.)
Public methodCode exampleRemoveChild
Removes the specified child node.
(Inherited from CompositeNode.)
Public methodCode exampleRemoveSmartTags
Removes all SmartTag descendant nodes of the current node.
(Inherited from CompositeNode.)
Public methodCode exampleSelectNodes
Selects a list of nodes matching the XPath expression.
(Inherited from CompositeNode.)
Public methodCode exampleSelectSingleNode
Selects the first Node that matches the XPath expression.
(Inherited from CompositeNode.)
Public methodToString (Inherited from Object.)
Public methodCode exampleToString(SaveFormat)
Exports the content of the node into a string in the specified format.
(Inherited from Node.)
Public methodCode exampleToString(SaveOptions)
Exports the content of the node into a string using the specified save options.
(Inherited from Node.)
Remarks

Cell can only be a child of a Row.

Cell can contain block-level nodes Paragraph and Table.

A minimal valid cell needs to have at least one Paragraph.

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");
}
Examples
Shows how to build a nested table without using DocumentBuilder.
public void CreateNestedTable()
{
    Document doc = new Document();

    // Create the outer table with three rows and four columns
    Table outerTable = CreateTable(doc, 3, 4, "Outer Table");
    // Add it to the document body
    doc.FirstSection.Body.AppendChild(outerTable);

    // Create another table with two rows and two columns
    Table innerTable = CreateTable(doc, 2, 2, "Inner Table");
    // Add this table to the first cell of the outer table
    outerTable.FirstRow.FirstCell.AppendChild(innerTable);

    doc.Save(ArtifactsDir + "Table.CreateNestedTable.doc");

    Assert.AreEqual(2, doc.GetChildNodes(NodeType.Table, true).Count); // ExSkip
}

/// <summary>
/// Creates a new table in the document with the given dimensions and text in each cell.
/// </summary>
private static Table CreateTable(Document doc, int rowCount, int cellCount, string cellText)
{
    Table table = new Table(doc);

    // Create the specified number of rows
    for (int rowId = 1; rowId <= rowCount; rowId++)
    {
        Row row = new Row(doc);
        table.AppendChild(row);

        // Create the specified number of cells for each row
        for (int cellId = 1; cellId <= cellCount; cellId++)
        {
            Cell cell = new Cell(doc);
            row.AppendChild(cell);
            // Add a blank paragraph to the cell
            cell.AppendChild(new Paragraph(doc));

            // Add the text
            cell.FirstParagraph.AppendChild(new Run(doc, cellText));
        }
    }

    // You can add title and description to your table only when added at least one row to the table first
    // This properties are meaningful for ISO / IEC 29500 compliant DOCX documents(see the OoxmlCompliance class)
    // When saved to pre-ISO/IEC 29500 formats, the properties are ignored
    table.Title = "Aspose table title";
    table.Description = "Aspose table description";

    return table;
}
See Also