java excel xlsx 增加数据验证

隐藏表下拉框

bash 复制代码
   // 创建隐藏工作表存储下拉框数据
            String hiddenSheetName = "HiddenSheet"+System.currentTimeMillis();
            Sheet hiddenSheet =  workbook.createSheet(hiddenSheetName);
            //设置隐藏sheet
            workbook.setSheetHidden(workbook.getSheetIndex(hiddenSheetName), true);
            for (int i = 0; i < dropdownList.size(); i++) {
                Row row = hiddenSheet.createRow(i);
                Cell cityCellHidden = row.createCell(0);
                cityCellHidden.setCellValue(dropdownList.get(i));
            }
            // 获取DataValidationHelper对象
            DataValidationHelper helper = mainSheet.getDataValidationHelper();
            // 定义数据验证的范围
            CellRangeAddressList addressList = new CellRangeAddressList(startRow,  65535, columnIndex, columnIndex);
            // 创建数据验证约束 公式可以写成"Sheet2!$A$1:$A$10"。
            DataValidationConstraint constraint = helper.createFormulaListConstraint(hiddenSheetName+"!$A$1:$A$"+dropdownList.size());
            //生成验证规则
            DataValidation validation = helper.createValidation(constraint, addressList);
            // 设置错误提示信息
            validation.setShowErrorBox(true);
            validation.setSuppressDropDownArrow(true);

下拉框

bash 复制代码
   DataValidationHelper helper = mainSheet.getDataValidationHelper();
            DataValidationConstraint constraint = helper.createExplicitListConstraint(
                    dropdownList.toArray(new String[0]));
            CellRangeAddressList addressList = new CellRangeAddressList(startRow, 65535, columnIndex, columnIndex);
            DataValidation validation = helper.createValidation(constraint, addressList);
            validation.setShowErrorBox(true);
            validation.setSuppressDropDownArrow(true);
            mainSheet.addValidationData(validation);

时间格式及范围验证

bash 复制代码
       // 创建数据验证助手
        DataValidationHelper helper = sheet.getDataValidationHelper();
        CellRangeAddressList dateAddressList = new CellRangeAddressList(firstRow, lastRow, columnIndex, columnIndex);
        // 获取 LocalDateTime 的最小值
        LocalDateTime minLocalDateTime = LocalDateTime.MIN;
        // 获取 LocalDateTime 的最大值
        LocalDateTime maxLocalDateTime = LocalDateTime.MAX;
        DataValidationConstraint dataValidationConstraint = helper.createDateConstraint(DataValidationConstraint.OperatorType.BETWEEN,
                minLocalDateTime.format(DateTimeFormatter.ofPattern(dateFormat)),
                maxLocalDateTime.format(DateTimeFormatter.ofPattern(dateFormat)),
                dateFormat);
        DataValidation validation = helper.createValidation(dataValidationConstraint, dateAddressList);
        validation.setShowErrorBox(true);
        validation.setSuppressDropDownArrow(true);
        validation.setShowErrorBox(true);
        validation.createErrorBox("标题","内容");
        sheet.addValidationData(validation);

integer 验证

bash 复制代码
// 创建数据验证助手
        DataValidationHelper helper = sheet.getDataValidationHelper();
        CellRangeAddressList intAddressList = new CellRangeAddressList(firstRow, lastRow, columnIndex, columnIndex);
        // 获取 LocalDateTime 的最大值
        LocalDateTime maxLocalDateTime = LocalDateTime.MAX;
        DataValidationConstraint dataValidationConstraint = helper.createIntegerConstraint(DataValidationConstraint.OperatorType.BETWEEN,
                String.valueOf(Integer.MIN_VALUE),
                String.valueOf(Integer.MAX_VALUE));
        DataValidation validation = helper.createValidation(dataValidationConstraint, intAddressList);
        validation.setShowErrorBox(true);
        validation.setSuppressDropDownArrow(true);
        sheet.addValidationData(validation);
相关推荐
战族狼魂6 分钟前
CSGO 皮肤交易平台后端 (Spring Boot) 代码结构与示例
java·spring boot·后端
Tttian6221 小时前
Python办公自动化(3)对Excel的操作
开发语言·python·excel
xyliiiiiL1 小时前
ZGC初步了解
java·jvm·算法
杉之2 小时前
常见前端GET请求以及对应的Spring后端接收接口写法
java·前端·后端·spring·vue
蹦蹦跳跳真可爱5892 小时前
Python----机器学习(KNN:使用数学方法实现KNN)
人工智能·python·机器学习
hycccccch2 小时前
Canal+RabbitMQ实现MySQL数据增量同步
java·数据库·后端·rabbitmq
独好紫罗兰2 小时前
洛谷题单2-P5713 【深基3.例5】洛谷团队系统-python-流程图重构
开发语言·python·算法
天天向上杰3 小时前
面基JavaEE银行金融业务逻辑层处理金融数据类型BigDecimal
java·bigdecimal
请来次降维打击!!!3 小时前
优选算法系列(5.位运算)
java·前端·c++·算法
用键盘当武器的秋刀鱼3 小时前
springBoot统一响应类型3.5.1版本
java·spring boot·后端