Search code examples
javaapache-poixssf

Apache POI - Constraint on text length to be 8 OR 10


I would like to add the folloqing constraints to a column:

  1. text length should be of length 8 OR 10

  2. all the digits should be number BUT the type of the column is string

Do you think is possible?

Here I have set the column to be string

DataFormat fmt = wb.createDataFormat();
CellStyle textStyle = wb.createCellStyle();
textStyle.setDataFormat(fmt.getFormat("@"));
sheet.setDefaultColumnStyle(1, textStyle);

Then I have added the following constraint

XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createTextLengthConstraint(ComparisonOperator.EQUAL, "8", null);
CellRangeAddressList addressList = new CellRangeAddressList(-1, -1, 1, 1);
XSSFDataValidation validation = (XSSFDataValidation)dvHelper.createValidation(dvConstraint, addressList);
validation.setShowErrorBox(true);
sheet.addValidationData(validation);

but is not working and I don't know how to add the constraint that to be equal to 8 or 10, nor that the digits should be numbers.

Thanks a lot


Solution

  • I did it like this, t's not exactly like i wanted but is acceptable:

    dvConstraint = (XSSFDataValidationConstraint) dvHelper.createNumericConstraint(ValidationType.TEXT_LENGTH, OperatorType.BETWEEN, "8", "10");
    addressList = new CellRangeAddressList(-1, -1, 1, 1);
    validation = (XSSFDataValidation)dvHelper.createValidation(dvConstraint, addressList);
    validation.setShowErrorBox(true);
    sheet2.addValidationData(validation);