XDoc.Word
Features
Tech Specs
How-to C#
Pricing

C# Word Library
C# Word - Table Cell Processing in C#.NET


Online Tutorial for Users to Set and Modify Table Cells in Word Document





C#.NET Word document table cell processing Interface control (XDoc.Word). It allows users to process table cells on an existing Word file.


Create and Add Cells in Table



The following demo code will show how to create a table cell and add to table.

String docFilePath = @"";
//Open the document
DOCXDocument document = DOCXDocument.Open(docFilePath);
//Get the main document
IDocument doc = document.GetDocument();
//Create a table in body
ITable table = doc.CreateTable(3, 3);
//Create a table row for table
ITable row = table.CreateRow(3);
//Creta a table cell for table row
ITableCell cell = row.CreateCell();
//Save the document
doc.Save(@"");


Properties Setup in Table Cell



In C#.NET applications, users can set and modify property of each cell in table by using following sample code.

String docFilePath = @"";
//Open the document
DOCXDocument document = DOCXDocument.Open(docFilePath);
//Get the main document
IDocument doc = document.GetDocument();
//Create a Table for document
ITable table = doc.CreateTable(3, 3);
//Get all rows in table
List<ITableRow> rows = table.GetRows();
//Get first row
ITableRow row = rows[0];
//Get all cells in first row
List<ITableCell> cells = row.GetCells();
//Get first cell
ITableCell cell = cells[0];
//Set properties for cell
//Cell width fit text
cell.SetFitText(true);
//Text flow direction
cell.SetTextFlowDirection(TextDirection.LrTbV);
//Shadow fill Color
cell.SetShadowFillColor(false, Color.Red);
//Cell width
cell.SetCellWidth(200);
//MORE TODO:
//
//
doc.Save(@"");


Create, Add and Modify Paragraph in Table Cell



Sometimes you may need to create some text content or nest table in Word document, the following demo code will show you how to create a text paragraph in table cell.

String docFilePath = @"";
//Open the document
DOCXDocument document = DOCXDocument.Open(docFilePath);
//Get the main document
IDocument doc = document.GetDocument();
//Create a Table for document
ITable table = doc.CreateTable(3, 3);
//Get all rows in table
List<ITableRow> rows = table.GetRows();
//Get first row
ITableRow row = rows[0];
//Get all cells in first row
List<ITableCell> cells = row.GetCells();
//Get first cell
ITableCell cell = cells[0];
//Create text content for cell
IParagraph paragraph = cell.CreateParagraph();
IRun run = paragraph.CreateARun();
run.CreateText("This is the first cell in table");
doc.Save(@"");


Create, Add and Modify Nest Table in Table Cell



The following demo code shows how to create nest tables in a cell.

String docFilePath = @"";
//Open the document
DOCXDocument document = DOCXDocument.Open(docFilePath);
//Get the main document
IDocument doc = document.GetDocument();
//Create a Table for document
ITable table = doc.CreateTable(3, 3);
//Get all rows in table
List<ITableRow> rows = table.GetRows();
//Get first row
ITableRow row = rows[0];
//Get all cells in first row
List<ITableCell> cells = row.GetCells();
//Get first cell
ITableCell cell = cells[0];
//Create a nest table with 2 columns and 2 rows for cell
ITable nestTable = cell.CreateTable(2, 2);

doc.Save(@"");