Document Solutions for Excel, .NET Edition | Document Solutions
Features / Table / Add and Delete Table Columns and Rows
In This Topic
    Add and Delete Table Columns and Rows
    In This Topic

    You can add and delete columns and rows of a table using the methods and properties of the following interfaces:

    Add and Delete Single Column

    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.

    C#
    Copy Code
    //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();

    Add and Delete Multiple Columns

    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.

    C#
    Copy Code
    //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);

    Add and Delete Single Row

    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.

    C#
    Copy Code
    //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();


    Add and Delete Multiple Rows

    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.

    C#
    Copy Code
     //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);