How to add a row to an existing word table in C#

With the help of Spire.Doc, developers can easily add new table to the word document in C# and VB.NET. Spire.Doc offers a method of table.AddRow() to enable developers to insert the new rows easily with styles or not at the bottom of the table. It also offers a method of table.Rows.Insert(int index, TableRow row) to enable developers to insert a new row at any index as we want. This article will focus on demonstrating how to add new rows to an existing word table in C#.

Firstly, please view the original word table:

How to add a row to an existing word table in C#

Note: Before Start, please download the latest version of Spire.Doc and add Spire.Doc.dll in the bin folder as the reference of Visual Studio.

Step 1: Create a new word document and load the document from file.

Document doc = new Document();
doc.LoadFromFile("sample.docx");

Step 2: Get the first table from the word document.

Table table = doc.Sections[0].Tables[0] as Spire.Doc.Table;

Step 3: Insert a new row as the third row.

TableRow row = table.AddRow();
table.Rows.Insert(2, row);

Step 4: Add two rows to the table at the end, one with format, the other one without format.

table.AddRow(true,2);
table.AddRow(false, 2);

Step 5: Save the document to file and set its file format.

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

Effective screenshot after adding rows to the word table:

How to add a row to an existing word table in C#

Full codes:

{
    Document doc = new Document();
    doc.LoadFromFile("sample.docx");
    Table table = doc.Sections[0].Tables[0] as Spire.Doc.Table;

    //Insert a new row as the third row
    TableRow row = table.AddRow();
    table.Rows.Insert(2, row);

    //Add two rows at the end of the table
    table.AddRow(true, 2);
    table.AddRow(false, 2);

    doc.SaveToFile("Result.docx", FileFormat.Docx2013);
              
}