Backend Development 6 min read

Create Excel Dropdown Lists in Java with Free Spire.XLS

This guide shows how to generate Excel dropdown lists programmatically in Java using the free Spire.XLS library, covering two approaches—direct string arrays and cell‑range data sources—complete with sample code and screenshots.

Java Captain
Java Captain
Java Captain
Create Excel Dropdown Lists in Java with Free Spire.XLS

Dropdown lists (combo boxes) ensure users select only from predefined options, reducing input errors and saving time. In Microsoft Excel this is done via Data Validation, but you can also create them programmatically in Java using third‑party libraries.

Free Java control for Excel : Free Spire.XLS for Java (download the JAR manually or add it via Maven).

Method 1: Create a dropdown list from a string array

This method uses the IDataValidation interface’s setValue() method to define an array of strings as the list items, and sets isSuppressDropDownArrow(false) to show the arrow.

<code>import com.spire.xls.*;
import java.awt.*;

public class ExcelDropdownList {
    public static void main(String[] args) {
        // Create Workbook object
        Workbook workbook = new Workbook();
        // Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);
        // Add text to specific cells
        sheet.getCellRange("B2").setValue("职员");
        sheet.getCellRange("B3").setValue("张三");
        sheet.getCellRange("C2").setValue("部门");
        // Set font and cell style
        sheet.getCellRange("B2:C2").getStyle().getFont().isBold(true);
        sheet.getCellRange("B2:C2").getStyle().getFont().setColor(Color.BLUE);
        sheet.getCellRange("B2:C3").getStyle().getFont().setSize(11);
        sheet.getCellRange("B2:C3").setRowHeight(18);
        sheet.getCellRange("B2:C3").setColumnWidth(12);
        // Set dropdown list values
        sheet.getCellRange("C3").getDataValidation().setValues(new String[]{"财务部", "采购部", "销售部", "行政部"});
        // Enable the dropdown arrow
        sheet.getCellRange("C3").getDataValidation().isSuppressDropDownArrow(false);
        // Save the result file
        workbook.saveToFile("Excel下拉列表.xlsx", ExcelVersion.Version2013);
    }
}
</code>

Method 2: Create a dropdown list from a cell range

This approach uses the Validation class’s setDataRange() method to specify a range of cells as the data source, making it easy to update the list by changing the cell values.

<code>import com.spire.xls.*;
import java.awt.*;

public class DropdownList {
    public static void main(String[] args) {
        // Create Workbook object
        Workbook workbook = new Workbook();
        // Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);
        // Add text to specific cells
        sheet.getCellRange("B2").setValue("职员");
        sheet.getCellRange("B3").setValue("张三");
        sheet.getCellRange("C2").setValue("部门");
        sheet.getCellRange("A11").setValue("财务部");
        sheet.getCellRange("A12").setValue("采购部");
        sheet.getCellRange("A13").setValue("销售部");
        sheet.getCellRange("A14").setValue("行政部");
        // Set font and cell style
        sheet.getCellRange("B2:C2").getStyle().getFont().isBold(true);
        sheet.getCellRange("B2:C2").getStyle().getFont().setColor(Color.BLUE);
        sheet.getCellRange("B2:C3").getStyle().getFont().setSize(11);
        sheet.getCellRange("B2:C3").setRowHeight(18);
        sheet.getCellRange("B2:C3").setColumnWidth(12);
        // Use cell range A11:A14 as the dropdown source
        sheet.getCellRange("C3:C4").getDataValidation().setDataRange(sheet.getCellRange("A11:A14"));
        // Save the result file
        workbook.saveToFile("Excel下拉列表2.xlsx", ExcelVersion.Version2013);
    }
}
</code>

The free Spire.XLS for Java library supports many Excel operations, though it has some limitations.

JavaExceldata validationSpire.XLSDropdown List
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.