How to Create Data Validation in Excel using Java

This brief tutorial will teach you how to create data validation in Excel using Java. It provides resources to establish the environment, a list of steps to be followed to accomplish the task, and a runnable sample code to insert data validation in Excel using Java. You will get a description of the code and in the end, create an XLS or XLSX file having a validation rule in it.

Steps to Create Data Validation Rule in Excel using Java

  1. Establish the environment to add Aspose.Cells for Java to insert validation
  2. Create a new workbook and add a worksheet for adding reference data to it
  3. Instantiate a range class object and set its name and desired list of values
  4. Create a new validation in the validations collection of the target sheet and set its cell area
  5. Set other properties of the validation
  6. Save the workbook on the disk having validation in it

These steps explain the process of how to add data validation in Excel using Java. You have to create a workbook, create a worksheet in it to add list data, instantiate a range class object and set it in a newly created validation. In the end, set different properties of the validation and save the resultant workbook on the disk.

Code to Create a Data Validation List in Excel using Java

import com.aspose.cells.CellArea;
import com.aspose.cells.License;
import com.aspose.cells.OperatorType;
import com.aspose.cells.Range;
import com.aspose.cells.Validation;
import com.aspose.cells.ValidationAlertType;
import com.aspose.cells.ValidationCollection;
import com.aspose.cells.ValidationType;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
public class Main {
public static void main(String[] args) throws Exception { // Main function to add validation to a worksheet
// Load a license
License lic = new License();
lic.setLicense("Aspose.Total.lic");
// Instantiate a workbook
Workbook workbook = new Workbook();
// Access the first sheet
Worksheet worksheet1 = workbook.getWorksheets().get(0);
// Create another sheet for reference data and get access to it
int i = workbook.getWorksheets().add();
Worksheet worksheet2 = workbook.getWorksheets().get(i);
// Create a range for the reference list
Range referenceRange = worksheet2.getCells().createRange("E1", "E4");
// Set the name property of the above-created range
referenceRange.setName("ReferenceRange");
// Fill the reference list to be used for validation
referenceRange.get(0,0).putValue("Tiny");
referenceRange.get(1, 0).putValue("Small");
referenceRange.get(2, 0).putValue("Medium");
referenceRange.get(3, 0).putValue("Large");
// Get a reference to the validations collection on the first sheet
ValidationCollection validations = worksheet1.getValidations();
// Create a cell Area where validation is to be implemented
CellArea area = new CellArea();
area.StartRow = 0;
area.EndRow = 4;
area.StartColumn = 0;
area.EndColumn = 0;
// Create a new validation for the given cell area defined above
validations.add(area);
Validation validation = validations.get(validations.add(area));
// Set type of validation
validation.setType (ValidationType.LIST);
// Set the type of operator
validation.setOperator(OperatorType.NONE);
// Set flag for in-cell drop-down
validation.setInCellDropDown(true);
// Set the formula by providing reference data range name
validation.setFormula1("=ReferenceRange");
// Enable the flag to show an error
validation.setShowError(true);
// Set the type of alert on error
validation.setAlertStyle(ValidationAlertType.STOP);
// Set the title
validation.setErrorTitle("Error Title");
// Set the message to be shown when an error is raised
validation.setErrorMessage("Please select data from the list");
// Save the output file
workbook.save("output.xls");
System.out.println("Done!");
}
}

This code demonstrates how to insert data validation in Excel using Java. It uses a Range class object to set the range name and set of values that the validation object will use. Each worksheet contains a collection of validations where new validation is added along with the properties and effective cell area.

This tutorial has guided us on how to insert validation in Excel using Java. If you want to learn the process to apply filters in Excel, refer to the article on how to apply filter in Excel using Java.

 English