Wednesday, 27 May 2020 01:57

Sort Excel Data in Java

Written by  support iceblue
Rate this item
(0 votes)

This article demonstrates how to sort data within a cell range using Spire.XLS for Java.

import com.spire.xls.Worksheet;
import com.spire.xls.Workbook;
import com.spire.xls.OrderBy;
import com.spire.xls.ExcelVersion;

public class SortData {

    public static void main(String[] args) {

        //Create a Workbook object
        Workbook workbook = new Workbook();

        //Load the sample Excel file
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Specify the column index and the sort mode. The column index starts from 0.
        workbook.getDataSorter().getSortColumns().add(1,OrderBy.Ascending);

        //Specify the range to sort
        workbook.getDataSorter().sort(sheet.getCellRange("A1:F9"));

        //Save the document
        workbook.saveToFile("SortData.xlsx", ExcelVersion.Version2016);
    }
}

Sort Excel Data in Java

Additional Info

  • tutorial_title:
Last modified on Wednesday, 27 May 2020 02:01