使用POI库(java)向Excel的单列添加多个验证

时间:2017-07-17 10:07:48

标签: java apache-poi

我想在Excel工作表的单个列中添加多个验证。例如, 有一个电子邮件ID列。首先,我想检查如果它是空的,那么我想检查它是否是有效的电子邮件ID。我使用下面的代码来检查两者。但它不起作用。

    XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
    XSSFDataValidationConstraint dvConstraint111 = (XSSFDataValidationConstraint) dvHelper.createCustomConstraint(
            "=COUNTA($C$2:C2)>ROW()-2");
    CellRangeAddressList addressList111 = new CellRangeAddressList(0, 9, 2, 2);
    XSSFDataValidation validation111 = (XSSFDataValidation) dvHelper.createValidation(dvConstraint111, addressList111);
    validation111.setShowErrorBox(true);
    validation111.createErrorBox("Warning!!!", "Empty cells not allowed for Email column");
    sheet.addValidationData(validation111);

    XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createCustomConstraint(
            "=AND(NOT(ISERROR(FIND(\"@\",C1))),NOT(ISERROR(FIND(\".\",C1))),ISERROR(FIND(\" \",C1)))");
    CellRangeAddressList addressList = new CellRangeAddressList(0, 9, 2, 2);
    XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
    validation.setShowErrorBox(true);
    validation.createErrorBox("Warning!!!", "Please enter valid email id");
    sheet.addValidationData(validation);

0 个答案:

没有答案