gpt4 book ai didi

Java - Apache POI 整数验证

转载 作者:行者123 更新时间:2023-12-01 14:04:45 29 4
gpt4 key购买 nike

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");

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

这是我的引用。 Busy Developer

最佳答案

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

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

关于Java - Apache POI 整数验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18995778/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com