How to create nested tables in word template and how to add rows to nested tables in c# ?
Thanks for your inquiry.
Please move the cursor to the desired table’s cell and create the table. Following example shows how to insert a nested table using DocumentBuilder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Build the outer table.
Cell cell = builder.InsertCell();
builder.Writeln("Outer Table Cell 1");
builder.InsertCell();
builder.Writeln("Outer Table Cell 2");
// This call is important in order to create a nested table within the first table
// Without this call the cells inserted below will be appended to the outer table.
builder.EndTable();
// Move to the first cell of the outer table.
builder.MoveTo(cell.FirstParagraph);
// Build the inner table.
builder.InsertCell();
builder.Writeln("Inner Table Cell 1");
builder.InsertCell();
builder.Writeln("Inner Table Cell 2");
builder.EndTable();
// Save the document to disk.
doc.Save("output.docx");
Please use the following code example to add the row at the end of table. Hope this helps you.
Document doc = new Document(MyDir + "in.docx");
// Retrieve the table
Table table = (Table)doc.GetChild(NodeType.Table, 1, true);
// Clone the last row in the table.
Row clonedRow = (Row)table.LastRow.Clone(true);
// Remove all content from the cloned row's cells. This makes the row ready for
// new content to be inserted into.
foreach (Cell cell in clonedRow.Cells)
cell.RemoveAllChildren();
// Add the row to the end of the table.
table.AppendChild(clonedRow);
doc.Save(MyDir + "Table.AddCloneRowToTable Out.doc");