Add Filters to Excel in Java
Tags: java, excel, filter
Published: 2020-03-19 02:59:48


---
title: Add Filters to Excel in Java
published: true
description: This article shows you how to add a text filter, a number filter, a color filter and a date filter to Excel in Java.
tags: java, excel, filter
---
When the data in Excel worksheet is very large, you can add "text filter", "number filter", "color filter" or "date filter" in a selected area to filter the data. The filtered worksheet displays the data you need and hides the rest.

In the worksheet shown below, you can add a text filter in the column "category" to make the worksheet display only one category of data; you can add a number filter in the column "quantity" to make the worksheet display the rows containing the number between 80-100; you can add a color filter to display the rows having background color; you can also add a date filter to the “date” column to display the data within the specific month.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tjdroa6iudm9e64hvovm.png)

Let’s get started by importing the Spire.Xls.jar file in your java application.

##Add Spire.Xls.jar as dependency

Method 1: Download [Free Spire.XLS for Java](https://www.e-iceblue.com/Download/xls-for-java-free.html) pack, unzip it and you’ll get Spire.Doc.jar file from the “lib” folder. Import the jar file in your project as a denpendency.

Method 2:  If you are creating a Maven project, you can easily add the jar dependency by adding the following configurations to the pom.xml.
```java
<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>2.2.0</version>
    </dependency>
</dependencies>
```
##Example 1. Text Filter
```java
//Create a Workbook object
Workbook wb = new Workbook();
//Load a sample Excel file
wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");
//Get the first worksheet
Worksheet sheet = wb.getWorksheets().get(0);
//Get the AutoFiltersCollection object
AutoFiltersCollection filters = sheet.getAutoFilters();
//Set the cell range where the filters will be added
filters.setRange(sheet.getCellRange(2,2,16,5));
//Add a filter to the second column of the selected range and set the filter criteria, which is the text "C"
filters.addFilter(1, "C");
//Execute filter function
filters.filter();
//Save to file
wb.saveToFile("TextFilter.xlsx", ExcelVersion.Version2013);
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dur0zv8expcbd1no1vbx.png)

##Example 2. Number Filter
```java
//Create a Workbook object
Workbook wb = new Workbook();
//Load a sample Excel file
wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");
//Get the first worksheet
Worksheet sheet = wb.getWorksheets().get(0);
//Get the AutoFiltersCollection object
AutoFiltersCollection filters = sheet.getAutoFilters();
//Set the cell range where the filters will be added
filters.setRange(sheet.getCellRange(2,2,16,5));
//Add a custom filter to the third column of the selected range and set the filter criteria, which is to select the number greater than or equal to 80 and less than or equal to 100
filters.customFilter(2, FilterOperatorType.GreaterOrEqual, 80, true, FilterOperatorType.LessOrEqual, 100);
//Execute filter function
filters.filter();
//Save to file
wb.saveToFile("NumberFilter.xlsx", ExcelVersion.Version2013);
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rvuk22lhcwlhe45da65z.png)

##Example 3. Color Filter
```java
//Create a Workbook object
Workbook wb = new Workbook();
//Load a sample Excel file
wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");
//Get the first worksheet
Worksheet sheet = wb.getWorksheets().get(0);
//Get the AutoFiltersCollection object
AutoFiltersCollection filters = sheet.getAutoFilters();
//Set the cell range where the filters will be added
filters.setRange(sheet.getCellRange(2,2,16,5));
//Add a color filter to the first column of the selected range and set the filter criteria, that is, the background color of cell B4
filters.addFillColorFilter(0, sheet.getCellRange("B4").getStyle().getColor());
//Execute filter function
filters.filter();
//Save to file
wb.saveToFile("ColorFilter.xlsx", ExcelVersion.Version2013);
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jgrrd0qfwntg4rzcpf6y.png)

##Example 4. Date Filter
```java
//Create a Workbook object
Workbook wb = new Workbook();
//Load a sample Excel file
wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");
//Get the first worksheet
Worksheet sheet = wb.getWorksheets().get(0);
//Get the AutoFiltersCollection object
AutoFiltersCollection filters = sheet.getAutoFilters();
//Set the cell range where the filters will be added
filters.setRange(sheet.getCellRange(2,2,16,5));
//Add a date filter in the fourth column of the selected range, using April 2019 as the filter criteria
filters.addDateFilter(3, DateTimeGroupingType.Month, 2019, 4, 1, 0, 0, 0 );
//Execute filter function
filters.filter();
//Save to file
wb.saveToFile("DateFilter.xlsx", ExcelVersion.Version2013);
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/c1dnh6sof2q6ddpelxos.png)