0
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(
                destinationSheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper
                .createIntegerConstraint(OperatorType.EQUAL, "0", "10");
CellRangeAddressList addressList = new CellRangeAddressList(7,
                destinationSheet.getLastRowNum() + 1, 9, destinationSheet
                        .getRow(0).getLastCellNum());
XSSFDataValidation validation = (XSSFDataValidation) dvHelper
                .createValidation(dvConstraint, addressList);

validation.setErrorStyle(ErrorStyle.STOP);
validation.createErrorBox("Error", "Only numeric values are allowed");

destinationSheet.addValidationData(validation);

我使用了上面的代码,它不起作用并且仍然接受单元格上的任何内容。如果我是对的,这条线就是罪魁祸首。

XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper
                    .createIntegerConstraint(OperatorType.EQUAL, "0", "10");

基本上,我希望 Cell 范围中的单元格只接受 0 和 10 并限制字母字符。我也试过OperatorType.BETWEEN但没有运气。

这是我的参考。忙碌的开发者

4

1 回答 1

1

每当用户输入无效数据时,上面的代码都不会显示错误。

XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(
                destinationSheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper
                .createIntegerConstraint(OperatorType.EQUAL, "0", "10");
CellRangeAddressList addressList = new CellRangeAddressList(7,
                destinationSheet.getLastRowNum() + 1, 9, destinationSheet
                        .getRow(0).getLastCellNum());
XSSFDataValidation validation = (XSSFDataValidation) dvHelper
                .createValidation(dvConstraint, addressList);

validation.setErrorStyle(ErrorStyle.STOP);
validation.createErrorBox("Error", "Only numeric values are allowed");
validation.setShowErrorBox(true); // The code is missing this line

destinationSheet.addValidationData(validation);

XSSF 中的实现与 HSSF 不同,HSSF 另一方面不需要线路validation.setShowErrorBox(true);工作。我在之前的实现中使用了 HSSF,导致未将 showErrorBox 设置为 true。

于 2013-09-25T06:47:16.163 回答