Click or drag to resize

TableCell Class

Represents a single cell in a table row, which contains the table’s content.
Inheritance Hierarchy
SystemObject
  SautinSoft.DocumentElement
    SautinSoft.Document.TablesTableCell

Namespace: SautinSoft.Document.Tables
Assembly: SautinSoft.Document (in SautinSoft.Document.dll) Version: 2024.3.11
Syntax
public sealed class TableCell : Element, 
	IContentElement

The TableCell type exposes the following members.

Constructors
 NameDescription
Public methodCode exampleTableCell(DocumentCore) Creates a new Table Cell.
Public methodCode exampleTableCell(DocumentCore, Block) Creates a new Table Cell with Block elements.
Public methodCode exampleTableCell(DocumentCore, IEnumerableBlock) Creates a new Table Cell with Block elements.
Top
Properties
 NameDescription
Public propertyBlocks The collection of Block elements (Paragraphs, Table, etc) inside this cell.
Public propertyCode exampleCellFormat Gets and sets cell formatting.
Public propertyCode exampleColumnSpan The attributes COLSPAN (“how many across”) and ROWSPAN (“how many down”) indicate how many columns or rows a cell should take up.
Public propertyCode exampleElementType Gets the ElementType of this element instance.
(Overrides ElementElementType)
Public propertyParent Gets the parent TableRow element.
Public propertyParentCollection Gets the parent TableCellCollection element.
Public propertyCode exampleRowSpan The attributes COLSPAN (“how many across”) and ROWSPAN (“how many down”) indicate how many columns or rows a cell should take up.
Top
Methods
 NameDescription
Public methodClone Clones the current TableCell element and returns its new copy.
Top
Example

See Developer Guide: How to modify an existing table

How to modify an existing table in C#
using System.IO;
using System.Linq;
using SautinSoft.Document;
using SautinSoft.Document.Tables;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            ModifyTable();
        }

        /// <summary>
        /// How to modify an existing table in a document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/modify-table.php
        /// </remarks>
        public static void ModifyTable()
        {
            string sourcePath = @"..\..\..\table.docx";
            string destPath = "Table modified.docx";

            // Load a document with a table.
            DocumentCore dc = DocumentCore.Load(sourcePath);

            // Find a first table in the document.
            Table table = (Table)dc.GetChildElements(true, ElementType.Table).First();

            // Set dashed borders and yellow background for all cells.
            for (int r = 0; r < table.Rows.Count; r++)
            {
                for (int c = 0; c < table.Rows[r].Cells.Count; c++)
                {
                    TableCell cell = table.Rows[r].Cells[c];
                    cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Dashed, Color.Black, 1);
                    cell.CellFormat.BackgroundColor = new Color("#FFCC00");
                }
            }

            // Save the document as DOCX.
            dc.Save(destPath, new DocxSaveOptions());

            // Show the source and the dest documents.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(sourcePath) { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(destPath) { UseShellExecute = true });
        }
    }
}
How to modify an existing table in VB.Net
Imports System
Imports System.IO
Imports System.Linq
Imports SautinSoft.Document
Imports SautinSoft.Document.Tables

Module Sample
    Sub Main()
        ModifyTable()
    End Sub
    ''' Get your free 30-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' How to modify an existing table in a document.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/modify-table.php
    ''' </remarks>
    Sub ModifyTable()
        Dim sourcePath As String = "..\..\..\table.docx"
        Dim destPath As String = "Table modified.docx"

        ' Load a document with a table.
        Dim dc As DocumentCore = DocumentCore.Load(sourcePath)

        ' Find a first table in the document.
        Dim table As Table = CType(dc.GetChildElements(True, ElementType.Table).First(), Table)

        ' Set dashed borders and yellow background for all cells.
        For r As Integer = 0 To table.Rows.Count - 1
            Dim c As Integer = 0
            Do While c < table.Rows(r).Cells.Count
                Dim cell As TableCell = table.Rows(r).Cells(c)
                cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Dashed, Color.Black, 1)
                cell.CellFormat.BackgroundColor = New Color("#FFCC00")
                c += 1
            Loop
        Next r

        ' Save the document as DOCX.
        dc.Save(destPath, New DocxSaveOptions())

        ' Show the source and the dest documents.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(sourcePath) With {.UseShellExecute = True})
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(destPath) With {.UseShellExecute = True})
    End Sub
End Module
See Also