How to Delete Rows and Columns from a Word Table in C#, VB.NET

Adding rows and columns are common tasks in Word table processing, on the contrary, sometimes we also have the requirement of deleting rows or columns from a table. This article demonstrates how to delete a row and a column from an existing Word table using Spire.Doc.

Below is the screenshot of the original table. Afterwards, we will remove the colored row and column from the table.

How to Delete Rows and Columns from a Word Table in C#, VB.NET

Detail steps:

Step 1: Instantiate a Document object and load the Word document.

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

Step 2: Get the table from the document.

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

Step 3: Delete the third row from the table.

table.Rows.RemoveAt(2);

Step 4: Delete the third column from the table.

for (int i = 0; i < table.Rows.Count; i++)
{
    table.Rows[i].Cells.RemoveAt(2);
}

Step 5: Save the document.

doc.SaveToFile("result.docx",FileFormat.docx2013);

Output:

How to Delete Rows and Columns from a Word Table in C#, VB.NET

Full code:

[C#]
using Spire.Doc;

namespace Delete_Rows_and_Columns
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("Sample.docx");
            Table table = doc.Sections[0].Tables[0] as Table;           
            table.Rows.RemoveAt(2);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                table.Rows[i].Cells.RemoveAt(2);
            }
            doc.SaveToFile("result.docx",FileFormat.docx2013);
        }
    }
}
[VB.NET]
Imports Spire.Doc

Namespace Delete_Rows_and_Columns
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New Document()
			doc.LoadFromFile("Sample.docx")
			Dim table As Table = TryCast(doc.Sections(0).Tables(0), Table)
			table.Rows.RemoveAt(2)
			For i As Integer = 0 To table.Rows.Count - 1
				table.Rows(i).Cells.RemoveAt(2)
			Next
			doc.SaveToFile("result.docx",FileFormat.docx2013);
		End Sub
	End Class
End Namespace