Created
October 26, 2018 15:31
-
-
Save AsposeCellsBlog/4dee263b69764f392a68908ac573d037 to your computer and use it in GitHub Desktop.
Use AutoFilter to Filter Excel Data - Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Directory path for input and output Excel files. | |
String dirPath = "D:/Download/"; | |
// Load the input Excel file containing the sample data. | |
Workbook wb = new Workbook(dirPath + "sampleUseAutoFilterToFilterExcelData.xlsx"); | |
// Access first worksheet. | |
Worksheet ws = wb.getWorksheets().get(0); | |
// Apply auto filter to the range. | |
ws.getAutoFilter().setRange("D3:G3"); | |
// Add filter to first column (i.e. Vehicle) inside the range - Criteria --> Bike | |
ws.getAutoFilter().addFilter(0, "Bike"); | |
// Add filter to first column (i.e. Vehicle) inside the range - Criteria --> Car | |
ws.getAutoFilter().addFilter(0, "Car"); | |
// Refresh the auto filter. | |
ws.getAutoFilter().refresh(); | |
// Add filter to second column (i.e. Color) inside the range - Criteria --> Green | |
ws.getAutoFilter().addFilter(1, "Green"); | |
// Add filter to second column (i.e. Color) inside the range - Criteria --> Blue | |
ws.getAutoFilter().addFilter(1, "Blue"); | |
// Refresh the auto filter. | |
ws.getAutoFilter().refresh(); | |
// Save the workbook in XLSX format. | |
// You can also save it to XLS or other formats. | |
wb.save(dirPath + "outputUseAutoFilterToFilterExcelData.xlsx", SaveFormat.XLSX); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment