Documents for Excel, Java Edition Documentation
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.

    Java
    Copy Code
    //Create first table
    ITable table1 = worksheet.getTables().add(worksheet.getRange("D3:I6"), true);
            
    //Create second table
    ITable table2 = worksheet.getTables().add(worksheet.getRange("A1:C6"), true);
    
    // Insert a table column before first column in first table
    table1.getColumns().add(0);
    
    // Insert a table column before first column in second table
    table2.getColumns().add(0);
    
    // Delete the first table column from the first table.
    worksheet.getTables().get(0).getColumns().get(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.

    Java
    Copy Code
    // Add table
    ITable table = worksheet.getTables().add(worksheet.getRange("A1:F7"), true);
    
    // Add two columns before first column
    table.getColumns().add(0, 2);
    // Delete three columns after second column
    table.getColumns().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.

    Java
    Copy Code
    // Insert a new row at the end of the first table
    table1.getRows().add();
    
    // Insert a new row at the end of the second table
    table2.getRows().add();
    
    // Delete the second row in the second table
    table2.getRows().get(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.

    Java
    Copy Code
    // Add table
    ITable table = worksheet.getTables().add(worksheet.getRange("A1:F7"), true);
    
    // Insert three rows after last row
    table.getRows().add(-1, 3);
    // Delete last table row
    table.getRows().delete(table.getRows().getCount() - 1, 1);