Create Filter in Excel using Java

When dealing with Excel documents containing a large amount of data, you can select the data you need quickly by filtering the data. This article will demonstrate how to create filter in an Excel file using Free Spire.XLS for Java.

Installation (2 Method)
1# Download the free API and unzip it, then add the Spire.Xls.jar file to your project as dependency.

2# You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<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>3.9.1</version>
</dependency>
</dependencies>

Code Snippet

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

public class CreateFilter {
public static void main(String[] args){
//Load an Excel file
Workbook workbook = new Workbook();
workbook.loadFromFile("sample3.xlsx");

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

//Create filter
sheet.getAutoFilters().setRange(sheet.getCellRange("A1:F1"));

//Save the result file
workbook.saveToFile("CreateFilter.xlsx", ExcelVersion.Version2010);
}
}

Output

Sharing Java Code