Cell
Contents
[
Hide
]Cell class
Represents a table cell.
To learn more, visit the Working with Tables documentation article.
public class Cell : CompositeNode
Constructors
Name | Description |
---|---|
Cell(DocumentBase) | Initializes a new instance of the Cell class. |
Properties
Name | Description |
---|---|
CellFormat { get; } | Provides access to the formatting properties of the cell. |
Count { get; } | Gets the number of immediate children of this node. |
CustomNodeId { get; set; } | Specifies custom node identifier. |
virtual Document { get; } | Gets the document to which this node belongs. |
FirstChild { get; } | Gets the first child of the node. |
FirstParagraph { get; } | Gets the first paragraph among the immediate children. |
HasChildNodes { get; } | Returns true if this node has any child nodes. |
override IsComposite { get; } | Returns true as this node can have child nodes. |
IsFirstCell { get; } | True if this is the first cell inside a row; false otherwise. |
IsLastCell { get; } | True if this is the last cell inside a row; false otherwise. |
LastChild { get; } | Gets the last child of the node. |
LastParagraph { get; } | Gets the last paragraph among the immediate children. |
NextCell { get; } | Gets the next Cell node. |
NextSibling { get; } | Gets the node immediately following this node. |
override NodeType { get; } | Returns Cell. |
Paragraphs { get; } | Gets a collection of paragraphs that are immediate children of the cell. |
ParentNode { get; } | Gets the immediate parent of this node. |
ParentRow { get; } | Returns the parent row of the cell. |
PreviousCell { get; } | Gets the previous Cell node. |
PreviousSibling { get; } | Gets the node immediately preceding this node. |
Range { get; } | Returns a Range object that represents the portion of a document that is contained in this node. |
Tables { get; } | Gets a collection of tables that are immediate children of the cell. |
Methods
Name | Description |
---|---|
override Accept(DocumentVisitor) | Accepts a visitor. |
override AcceptEnd(DocumentVisitor) | Accepts a visitor for visiting the end of the cell. |
override AcceptStart(DocumentVisitor) | Accepts a visitor for visiting the start of the cell. |
AppendChild<T>(T) | Adds the specified node to the end of the list of child nodes for this node. |
Clone(bool) | Creates a duplicate of the node. |
CreateNavigator() | Creates navigator which can be used to traverse and read nodes. |
EnsureMinimum() | If the last child is not a paragraph, creates and appends one empty paragraph. |
GetAncestor(NodeType) | Gets the first ancestor of the specified NodeType . |
GetAncestor(Type) | Gets the first ancestor of the specified object type. |
GetChild(NodeType, int, bool) | Returns an Nth child node that matches the specified type. |
GetChildNodes(NodeType, bool) | Returns a live collection of child nodes that match the specified type. |
GetEnumerator() | Provides support for the for each style iteration over the child nodes of this node. |
override GetText() | Gets the text of this node and of all its children. |
IndexOf(Node) | Returns the index of the specified child node in the child node array. |
InsertAfter<T>(T, Node) | Inserts the specified node immediately after the specified reference node. |
InsertBefore<T>(T, Node) | Inserts the specified node immediately before the specified reference node. |
NextPreOrder(Node) | Gets next node according to the pre-order tree traversal algorithm. |
PrependChild<T>(T) | Adds the specified node to the beginning of the list of child nodes for this node. |
PreviousPreOrder(Node) | Gets the previous node according to the pre-order tree traversal algorithm. |
Remove() | Removes itself from the parent. |
RemoveAllChildren() | Removes all the child nodes of the current node. |
RemoveChild<T>(T) | Removes the specified child node. |
RemoveSmartTags() | Removes all SmartTag descendant nodes of the current node. |
SelectNodes(string) | Selects a list of nodes matching the XPath expression. |
SelectSingleNode(string) | Selects the first Node that matches the XPath expression. |
ToString(SaveFormat) | Exports the content of the node into a string in the specified format. |
ToString(SaveOptions) | Exports the content of the node into a string using the specified save options. |
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 create a table.
Document doc = new Document();
Table table = new Table(doc);
doc.FirstSection.Body.AppendChild(table);
// Tables contain rows, which contain cells, which may have paragraphs
// with typical elements such as runs, shapes, and even other tables.
// Calling the "EnsureMinimum" method on a table will ensure that
// the table has at least one row, cell, and paragraph.
Row firstRow = new Row(doc);
table.AppendChild(firstRow);
Cell firstCell = new Cell(doc);
firstRow.AppendChild(firstCell);
Paragraph paragraph = new Paragraph(doc);
firstCell.AppendChild(paragraph);
// Add text to the first cell in the first row of the table.
Run run = new Run(doc, "Hello world!");
paragraph.AppendChild(run);
doc.Save(ArtifactsDir + "Table.CreateTable.docx");
Shows how to iterate through all tables in the document and print the contents of each cell.
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;
// We can use the "ToArray" method on a row collection to clone it into an array.
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;
// We can use the "ToArray" method on a cell collection to clone it into an array.
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");
}
Shows how to build a nested table without using a document builder.
public void CreateNestedTable()
{
Document doc = new Document();
// Create the outer table with three rows and four columns, and then add it to the document.
Table outerTable = CreateTable(doc, 3, 4, "Outer Table");
doc.FirstSection.Body.AppendChild(outerTable);
// Create another table with two rows and two columns and then insert it into the first table's first cell.
Table innerTable = CreateTable(doc, 2, 2, "Inner Table");
outerTable.FirstRow.FirstCell.AppendChild(innerTable);
doc.Save(ArtifactsDir + "Table.CreateNestedTable.docx");
}
/// <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);
for (int rowId = 1; rowId <= rowCount; rowId++)
{
Row row = new Row(doc);
table.AppendChild(row);
for (int cellId = 1; cellId <= cellCount; cellId++)
{
Cell cell = new Cell(doc);
cell.AppendChild(new Paragraph(doc));
cell.FirstParagraph.AppendChild(new Run(doc, cellText));
row.AppendChild(cell);
}
}
// You can use the "Title" and "Description" properties to add a title and description respectively to your table.
// The table must have at least one row before we can use these properties.
// These properties are meaningful for ISO / IEC 29500 compliant .docx documents (see the OoxmlCompliance class).
// If we save the document to pre-ISO/IEC 29500 formats, Microsoft Word ignores these properties.
table.Title = "Aspose table title";
table.Description = "Aspose table description";
return table;
}
See Also
- class CompositeNode
- namespace Aspose.Words.Tables
- assembly Aspose.Words