[]
You can add and delete columns and rows of a table using the methods and properties of the following interfaces:
ITableColumns Interface - Represents the table columns collection.
ITableRows Interface - Represents the table rows collection.
ITableColumn Interface - Represents an individual table column.
ITableRow Interface - Represents an individual table row.
To add and delete a table column, you can use the Add method of the ITableColumns interface and the Delete method of the ITableColumn interface respectively.
Refer to the following example code in order to add and delete a table column.
//Create first table
ITable table1 = worksheet.Tables.Add(worksheet.Range["D3:I6"], true);
//Create second table
ITable table2 = worksheet.Tables.Add(worksheet.Range["A1:C6"], true);
//Insert a table column before first column in first table
table1.Columns.Add(0);
//Insert a table column before first column in second table
table2.Columns.Add(0);
//Delete the first table column from the first table.
worksheet.Tables[0].Columns[0].Delete();
To add and delete multiple columns, you can use the Add and Delete methods of ITableColumns interface. These methods take the position of column and count of columns to be added or deleted as parameters.
Refer to the following example code in order to add and delete table columns.
//add table
ITable table = worksheet.Tables.Add(worksheet.Range["A1:F7"], true);
//add two columns before first column
table.Columns.Add(0, 2);
//delete three columns after second column
table.Columns.Delete(1, 3);
To add and delete a table row, you can use the Add method of the ITableRows interface and the Delete method of the ITableRow interface respectively.
Refer to the following example code in order to add and delete a table row.
//insert a new row at the end of the first table.
table1.Rows.Add();
//insert a new row at the end of the second table.
table2.Rows.Add();
//Delete the second row in the second table.
table2.Rows[1].Delete();
To add and delete multiple rows, you can use the Add and Delete methods of ITableRows interface. These methods take the position of row and count of rows to be added or deleted as parameters.
Refer to the following example code in order to add and delete table rows.
//add table
ITable table = worksheet.Tables.Add(worksheet.Range["A1:F7"], true);
//insert three rows after last row
table.Rows.Add(-1, 3);
//delete last table row
table.Rows.Delete(table.Rows.Count - 1, 1);