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

    With DsExcel Java, you can choose to apply sorting on a specific table in the worksheet. For executing the sort operation, you can use the getSort method of the ITable interface.

    The apply method is used to apply the selected sort state and display the results. In order to apply table sorting in a worksheet, refer to the following example code.

    Java
    Copy Code
    Workbook workbook = new Workbook();
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    ITable table = worksheet.getTables().add(worksheet.getRange("A1:E5"), true);
            
    // Assign values to range
    worksheet.getRange("A2").setValue(3);
    worksheet.getRange("A3").setValue(4);
    worksheet.getRange("A4").setValue(2);
    worksheet.getRange("A5").setValue(1);
    worksheet.getRange("B2").setValue(1);
    worksheet.getRange("B3").setValue(2);
    worksheet.getRange("B4").setValue(3);
    worksheet.getRange("B5").setValue(4);
    worksheet.getRange("F2").setValue("aaa");
    worksheet.getRange("F3").setValue("bbb");
    worksheet.getRange("F4").setValue("ccc");
    worksheet.getRange("F5").setValue("ddd");
    
    worksheet.getRange("B2:B5").getFormatConditions().addIconSetCondition();
    
    // Sort by column A firstly, then by column B.
    ValueSortField key1 = new ValueSortField(worksheet.getRange("A1:A2"), SortOrder.Ascending);
    IconSortField key2 = new IconSortField(worksheet.getRange("B1:B2"),
    workbook.getIconSets().get(IconSetType.Icon3Arrows).get(1), SortOrder.Descending);
    
    table.getSort().getSortFields().add(key1);
    table.getSort().getSortFields().add(key2);
    table.getSort().apply();

     

    See Also