Excel Data Validation

The following example shows how you can set Data Validation for certain cells.

GemBox.Spreadsheet supports reading and writing all types of Data Validations for XLSX and XLS file format.

Excel data validation set with GemBox.Spreadsheet for Java
Screenshot of Excel data validation set with GemBox.Spreadsheet for Java
import com.gembox.spreadsheet.*;

import java.time.LocalDateTime;

class Program {

    public static void main(String[] args) throws java.io.IOException {
        // If using the Professional version, put your serial key below.
        SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");

        ExcelFile workbook = new ExcelFile();
        ExcelWorksheet worksheet = workbook.addWorksheet("Data Validation");

        worksheet.getCell(0, 0).setValue("Data validation examples:");

        worksheet.getCell(2, 1).setValue("Decimal greater than 3.14 (on entire row 4):");
        DataValidation dataValidation = new DataValidation(worksheet.getRow(3).getCells());
        dataValidation.setType(DataValidationType.DECIMAL);
        dataValidation.setOperator(DataValidationOperator.GREATER_THAN);
        dataValidation.setFormula1(3.14);
        dataValidation.setInputMessageTitle("Enter a decimal");
        dataValidation.setInputMessage("Decimal should be greater than 3.14.");
        dataValidation.setErrorTitle("Invalid decimal");
        dataValidation.setErrorMessage("Value should be a decimal greater than 3.14.");
        worksheet.addDataValidation(dataValidation);
        worksheet.getCells().getSubrange("A4", "J4").setValue(3.15);

        worksheet.getCell(7, 1).setValue("List from B9 to B12 (on cell C8):");
        worksheet.getCell(8, 1).setValue("John");
        worksheet.getCell(9, 1).setValue("Fred");
        worksheet.getCell(10, 1).setValue("Hans");
        worksheet.getCell(11, 1).setValue("Ivan");
        dataValidation = new DataValidation(worksheet, "C8");
        dataValidation.setType(DataValidationType.LIST);
        dataValidation.setFormula1("=B9:B12");
        dataValidation.setInputMessageTitle("Enter a name");
        dataValidation.setInputMessage("Name should be from the list: John, Fred, Hans, Ivan.");
        dataValidation.setErrorStyle(DataValidationErrorStyle.WARNING);
        dataValidation.setErrorTitle("Invalid name");
        dataValidation.setErrorMessage("Value should be a name from the list: John, Fred, Hans, Ivan.");
        worksheet.addDataValidation(dataValidation);
        worksheet.getCell("C8").setValue("John");

        worksheet.getCell(13, 1).setValue("Date between 2011-01-01 and 2011-12-31 (on cell range C14:E15):");
        dataValidation = new DataValidation(worksheet.getCells().getSubrange("C14", "E15"));
        dataValidation.setType(DataValidationType.DATE);
        dataValidation.setOperator(DataValidationOperator.BETWEEN);
        dataValidation.setFormula1(LocalDateTime.of(2011, 1, 1, 0, 0));
        dataValidation.setFormula2(LocalDateTime.of(2011, 12, 31, 0, 0));
        dataValidation.setInputMessageTitle("Enter a date");
        dataValidation.setInputMessage("Date should be between 2011-01-01 and 2011-12-31.");
        dataValidation.setErrorStyle(DataValidationErrorStyle.INFORMATION);
        dataValidation.setErrorTitle("Invalid date");
        dataValidation.setErrorMessage("Value should be a date between 2011-01-01 and 2011-12-31.");
        worksheet.addDataValidation(dataValidation);
        worksheet.getCells().getSubrange("C14", "E15").setValue(LocalDateTime.of(2011, 1, 1, 0, 0));

        // Column width of 8, 55 and 15 characters.
        worksheet.getColumn(0).setWidth(8 * 256);
        worksheet.getColumn(1).setWidth(55 * 256);
        worksheet.getColumn(2).setWidth(15 * 256);
        worksheet.getColumn(3).setWidth(15 * 256);
        worksheet.getColumn(4).setWidth(15 * 256);

        workbook.save("Data Validation.%OutputFileType%");
    }
}

See also


Next steps

GemBox.Spreadsheet for Java is a component that enables you to read, write, edit and convert spreadsheet files from your applications using one simple API.

Download Buy