How to Create a Nested Table in Word in C#

2017-04-12 07:59:19 Written by  support iceblue
Rate this item
(0 votes)

A nested table is one table placed inside of another, where the larger table functions as a container for the smaller one. Nested tables allow you to arrange different sets of data in groups to show clients.

This article presents how we can create a nested table using Spire.Doc in C#.

Step 1: Create a new PDF document and add a section to it.

Document doc = new Document();
Section section = doc.AddSection();

Step 2: Add a table to the section.

Table table = section.AddTable(true);
table.ResetCells(2, 3);

Step 3: Adjust the column with.

table.Rows[0].Cells[0].Width = table.Rows[0].Cells[2].Width = 50F;
table.Rows[1].Cells[0].Width = table.Rows[1].Cells[2].Width = 50F;
table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow);

Step 4: Insert content to the cells of the table.

table[0, 0].AddParagraph().AppendText("SI.No.");
string text = "Earthwork excavation for foundation of buildings, water supply, "
             + "sanitary lines and electrical conduits either in pits or in "
             + "trenches 1.5m and above in width, in ordinary soil not exceeding "
             + "1.5m in depth including dressing the bottom and sides of pits and  "
             + "trenches, stacking the excavated soil clear.";
table[0, 1].AddParagraph().AppendText(text);
table[0, 2].AddParagraph().AppendText("Qty");  

Step 5: Insert a nested table to the cell (first row, second column).

Table nestedTable= table[0, 1].AddTable(true);
nestedTable.ResetCells(3, 4);
nestedTable.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitContents);

Step 6: Add content to nested cells.

nestedTable[0, 0].AddParagraph().AppendText("SI.No.");
nestedTable[0, 1].AddParagraph().AppendText("Item");
nestedTable[0, 2].AddParagraph().AppendText("Qty");
nestedTable[0, 3].AddParagraph().AppendText("Rate");
nestedTable[1, 0].AddParagraph().AppendText("1");
nestedTable[1, 1].AddParagraph().AppendText("Sand");
nestedTable[1, 2].AddParagraph().AppendText("30");
nestedTable[1, 3].AddParagraph().AppendText("45");
nestedTable[2, 0].AddParagraph().AppendText("2");
nestedTable[2, 1].AddParagraph().AppendText("Cement");
nestedTable[2, 2].AddParagraph().AppendText("30");
nestedTable[2, 3].AddParagraph().AppendText("50");

Step 7: Save the file.

doc.SaveToFile("Nested_Table.docx", FileFormat.Docx2013);

Output:

How to Create a Nested Table in Word in C#

Full Code:

//create a new pdf document
Document doc = new Document();
Section section = doc.AddSection();     

//add a table
Table table = section.AddTable(true);
table.ResetCells(2, 3);

//set column width
table.Rows[0].Cells[0].Width = table.Rows[0].Cells[2].Width = 50F;
table.Rows[1].Cells[0].Width = table.Rows[1].Cells[2].Width = 50F;
table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow);

//insert content to cells
table[0, 0].AddParagraph().AppendText("SI.No.");
string text = "Earthwork excavation for foundation of buildings, water supply, "
             + "sanitary lines and electrical conduits either in pits or in "
             + "trenches 1.5m and above in width, in ordinary soil not exceeding "
             + "1.5m in depth including dressing the bottom and sides of pits and  "
             + "trenches, stacking the excavated soil clear.";
table[0, 1].AddParagraph().AppendText(text);
table[0, 2].AddParagraph().AppendText("Qty");  

//add a nested table to cell(first row, second column)
Table nestedTable= table[0, 1].AddTable(true);
nestedTable.ResetCells(3, 4);
nestedTable.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitContents);

//add content to nested cells
nestedTable[0, 0].AddParagraph().AppendText("SI.No.");
nestedTable[0, 1].AddParagraph().AppendText("Item");
nestedTable[0, 2].AddParagraph().AppendText("Qty");
nestedTable[0, 3].AddParagraph().AppendText("Rate");
nestedTable[1, 0].AddParagraph().AppendText("1");
nestedTable[1, 1].AddParagraph().AppendText("Sand");
nestedTable[1, 2].AddParagraph().AppendText("30");
nestedTable[1, 3].AddParagraph().AppendText("45");
nestedTable[2, 0].AddParagraph().AppendText("2");
nestedTable[2, 1].AddParagraph().AppendText("Cement");
nestedTable[2, 2].AddParagraph().AppendText("30");
nestedTable[2, 3].AddParagraph().AppendText("50");

//save
doc.SaveToFile("Nested_Table.docx", FileFormat.Docx2013);

Additional Info

  • tutorial_title: Create a Nested Table in Word in C#
Last modified on Friday, 03 September 2021 03:53