POI - 无法为一系列单元格创建自定义验证

时间:2013-03-14 05:54:35

标签: constraints apache-poi validation

我需要检查用户是否在单元格A1:A10中输入了有效数字。在Excel中,我将选择单元格,然后创建自定义验证器并将公式设置为= isNumber(“$ A $ 1:$ A10”) 尝试使用POI做这件事让我全都陷入困境: 这是我尝试过的:

CellRangeAddressList addressList = new CellRangeAddressList(0, 10, 0, 0);
        XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
        DataValidationConstraint customConstraint = dvHelper.createCustomConstraint("isNumber(\"$A$0:$A$10\"");
        XSSFDataValidation validation = (XSSFDataValidation)dvHelper.createValidation(customConstraint, addressList);
        validation.setShowErrorBox(true);
        sheet.addValidationData(validation);

当我尝试在Excel中打开它时,我收到错误并且Excel禁用验证

提前谢谢 -anish

2 个答案:

答案 0 :(得分:0)

CellReference crStartCell = new CellReference(startRow, column, true, true); // 0-based row and column index
CellReference crEndCell = new CellReference(endRow, column, true, true);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createCustomConstraint("ISNUMBER("+crStartCell.formatAsString()+":"+crEndCell.formatAsString() +")");

答案 1 :(得分:0)

您可以将该Excel转换为csv,而不是使用SuperCSV执行验证。这将是长度,但更容易。