Document Solutions for Excel, Java Edition | Document Solutions
Features / Table / Modify Tables
In This Topic
    Modify Tables
    In This Topic

    While working with tables in DsExcel Java, you can configure it as per your spreadsheet requirements by modifying the table using the methods of the ITable interface.

    Modify table range

    You can modify the table range of your worksheet using the resize method of the ITable interface.

    In order to modify table range, refer to the following example code.

    Java
    Copy Code
    // Modify table range
    table.resize(worksheet.getRange("B1:E4"));

    Modify table areas

    You can modify the value of specific table areas by accessing its header range, data range and total range using the getHeaderRange method, getDataRange method and getTotalsRange method of the ITable interface.

    In order to modify table areas in your worksheet, refer to the following example code.

    Java
    Copy Code
    ITable table = worksheet.getTables().add(worksheet.getRange("A1:E5"), true);
    table.setShowTotals(true);
            
    // Populate table values
            
    worksheet.getRange("A2").setValue(3);
    worksheet.getRange("A3").setValue(4);
    worksheet.getRange("A4").setValue(2);
    worksheet.getRange("A5").setValue(1);
    worksheet.getRange("B2").setValue(32);
    worksheet.getRange("B3").setValue(41);
    worksheet.getRange("B4").setValue(12);
    worksheet.getRange("B5").setValue(16);
    worksheet.getRange("C2").setValue(3);
    worksheet.getRange("C3").setValue(4);
    worksheet.getRange("C4").setValue(15);
    worksheet.getRange("C5").setValue(18);
            
    // Table second column name set to "Age".
    worksheet.getTables().get(0).getHeaderRange().get(0, 1).setValue("Age");
    
    // "Age" Column's second row's value set to 23.
    worksheet.getTables().get(0).getDataRange().get(1, 1).setValue(23);
    
    // "Age" column's total row function set to average.
    worksheet.getTables().get(0).getTotalsRange().get(0, 1).setFormula("=SUBTOTAL(101,[Age])");

    Modify totals row of table column

    When you need to make changes to the total row's calculation function of a specific table column, you can use the setTotalsCalculation method of the ITableColumn interface.

    Refer to the following example code to modify column total row's calculation function.

    Java
    Copy Code
    Workbook workbook = new Workbook();
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    worksheet.getTables().add(worksheet.getRange("A1:E5"), true);
     
    // Populate table values
    worksheet.getRange("A2").setValue(3);
    worksheet.getRange("A3").setValue(4);
    worksheet.getRange("A4").setValue(2);
    worksheet.getRange("A5").setValue(1);
    worksheet.getRange("B2").setValue(32);
    worksheet.getRange("B3").setValue(41);
    worksheet.getRange("B4").setValue(12);
    worksheet.getRange("B5").setValue(16);
    worksheet.getRange("C2").setValue(3);
    worksheet.getRange("C3").setValue(4);
    worksheet.getRange("C4").setValue(15);
    worksheet.getRange("C5").setValue(18);
            
    // First table column's total row calculation fuction will be "=SUBTOTAL(101,[Column1])"
    worksheet.getTables().get(0).getColumns().get(0).setTotalsCalculation(TotalsCalculation.Average);
    worksheet.getTables().get(0).setShowTotals(true);