How to Work with Cell Validation in Java

How to Work with Cell Validation in Java

Aspose.Cells FOSS for Java constrains cell input with the Validation class and highlights cells conditionally with FormatCondition — both attached to a CellArea range on a worksheet. This guide covers adding a whole-number validation rule and a value-based conditional format.

Step-by-Step Guide

Step 1: Install the Package

<dependency>
  <groupId>org.aspose</groupId>
  <artifactId>aspose-cells-foss</artifactId>
  <version>26.7.0</version>
</dependency>

Step 2: Import Required Classes

import org.aspose.cells_foss.CellArea;
import org.aspose.cells_foss.FormatCondition;
import org.aspose.cells_foss.FormatConditionCollection;
import org.aspose.cells_foss.FormatConditionType;
import org.aspose.cells_foss.OperatorType;
import org.aspose.cells_foss.Style;
import org.aspose.cells_foss.Validation;
import org.aspose.cells_foss.ValidationType;
import org.aspose.cells_foss.Workbook;
import org.aspose.cells_foss.Worksheet;

Step 3: Add a Whole-Number Validation Rule

Worksheet.getValidations() returns the sheet’s ValidationCollection. Call add(cellArea) to register a rule over a range, then configure the returned Validation with setType(), setOperator(), setFormula1(), and setFormula2():

try (Workbook workbook = new Workbook()) {
    Worksheet sheet = workbook.getWorksheets().get(0);

    int validationIndex = sheet.getValidations().add(new CellArea(1, 0, 10, 0));
    Validation validation = sheet.getValidations().get(validationIndex);
    validation.setType(ValidationType.WHOLE_NUMBER);
    validation.setOperator(OperatorType.BETWEEN);
    validation.setFormula1("1");
    validation.setFormula2("100");

    workbook.save("validated.xlsx");
}

Step 4: Add Conditional Formatting

Worksheet.getConditionalFormattings() returns the sheet’s ConditionalFormattingCollection. Add an entry, declare the area it covers with addArea(...), then add a condition with addCondition(...) and modify the Style returned by getStyle():

int cfIndex = sheet.getConditionalFormattings().add();
FormatConditionCollection conditions = sheet.getConditionalFormattings().get(cfIndex);
conditions.addArea(CellArea.createCellArea("B2", "B11"));
int condIndex = conditions.addCondition(
    FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "1", "100");

FormatCondition condition = conditions.get(condIndex);
Style style = condition.getStyle();
style.getFont().setBold(true);
condition.setStyle(style);

workbook.save("conditional.xlsx");

Common Issues and Fixes

Conditional formatting style changes don’t take effect. Like Cell.getStyle(), FormatCondition.getStyle() returns a copy. Call FormatCondition.setStyle(style) again after modifying the font, borders, or fill on the returned object, as shown in Step 4.

Validation rule applies to the wrong cells. CellArea’s constructor takes zero-based row/column bounds in the order (firstRow, firstColumn, totalRows, totalColumns) — a range starting one row too high or too low usually means the first two arguments were swapped with the size arguments.

addCondition() with the wrong operator produces an always-true or always-false rule. OperatorType’s BETWEEN value requires both setFormula1() and a second bound; a single-bound comparison (for example, greater than) should use OperatorType’s GREATER_THAN value with only setFormula1() set.

Multiple validation rules on overlapping ranges behave unpredictably in Excel. ValidationCollection.add(...) does not check for overlap with existing rules — if two rules cover the same cell, Excel applies whichever rule was declared last. Remove the earlier rule with ValidationCollection.get(index) before adding a replacement if ranges overlap.

Frequently Asked Questions

What validation types are available?

ValidationType includes, among others, WHOLE_NUMBER for integer-only input, DECIMAL for decimal values, LIST for a predefined set of values, DATE for date values, and TEXT_LENGTH for character-count limits.

Can a conditional format use a formula instead of a fixed value?

Yes. FormatConditionCollection.addCondition(...) accepts any FormatConditionType; pass formula strings as the condition values the same way the fixed numeric bounds are passed in Step 4.

How many validation rules can one worksheet have?

There is no library-imposed limit beyond what ValidationCollection holds in memory — each call to add(cellArea) appends one more rule.

Do validation and conditional formatting round-trip correctly on save and reload?

Yes, both are read back into the same ValidationCollection and ConditionalFormattingCollection structures when the saved file is reopened with new Workbook(fileName).

See Also

 English