Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AsposeCellsBlog/4dee263b69764f392a68908ac573d037 to your computer and use it in GitHub Desktop.
Save AsposeCellsBlog/4dee263b69764f392a68908ac573d037 to your computer and use it in GitHub Desktop.
Use AutoFilter to Filter Excel Data - Java
// 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