• 002-poi-excel-导出设置单元格数据校验规则、筛选功能


    一、数据验证概述

    推荐以下操作在2007之后操作

    1.1、查看excel的数据验证

    1、进入

      

    2、设置规则

          

    通过验证条件允许,可以看到是每个单元格默认只成立一种条件

    1.2、POI代码开发-数据验证

    1.2.1、两个数之间

        public void excelRuleNumberBetween(Sheet sheet, int min, int max, int firstRow, int lastRow, int firstCol, int lastCol){
            DataValidationHelper helper = sheet.getDataValidationHelper();
            CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);//设置行列范围
            //设置数据
            DataValidationConstraint constraint = helper.createIntegerConstraint(DataValidationConstraint.OperatorType.BETWEEN,
                    String.valueOf(min),String.valueOf(max));
            DataValidation dataValidation = helper.createValidation(constraint, addressList);
            dataValidation.createErrorBox("输入值类型或大小有误", String.format("请输入%s~%s之间的数值",min,max));
            //处理Excel兼容性问题
            if(dataValidation instanceof XSSFDataValidation) {
                dataValidation.setSuppressDropDownArrow(true);
                dataValidation.setShowErrorBox(true);
            }else {
                dataValidation.setSuppressDropDownArrow(false);
            }
            sheet.addValidationData(dataValidation);
        }

    1.2.2、选择【序列】

        public void excelRuleSelect(Sheet sheet, String[] rule, int firstRow, int lastRow, int firstCol, int lastCol) {
            DataValidationHelper helper = sheet.getDataValidationHelper();
            CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
            DataValidationConstraint constraint = helper.createExplicitListConstraint(rule);
            DataValidation dataValidation = helper.createValidation(constraint, addressList);
            dataValidation.createErrorBox("输入有误", "请选择下拉参数");
            if (dataValidation instanceof XSSFDataValidation) {
                dataValidation.setSuppressDropDownArrow(true);
                dataValidation.setShowErrorBox(true);
            } else {
                dataValidation.setSuppressDropDownArrow(false);
            }
    
            sheet.addValidationData(dataValidation);
        }

    1.2.3、列唯一

      使用excel设置

        

      POI设置

        public void excelRuleUniqueue(Sheet sheet, int firstRow, int lastRow, int firstCol, int lastCol) {
            Row row = sheet.getRow(0);
            Cell cell = row.getCell(firstCol);
            String r = ((XSSFCell) cell).getCTCell().getR();
            r = r.substring(0, 1);
            DataValidationHelper helper = sheet.getDataValidationHelper();
            CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
            //唯一
            DataValidationConstraint constraint = helper.createCustomConstraint(MessageFormat.format("COUNTIF({0}:{0},{0}2)=1",r));
            DataValidation dataValidation = helper.createValidation(constraint, addressList);
            dataValidation.createErrorBox("错误:", "赋值属性列不允许重复");
            dataValidation.setShowErrorBox(true);
            dataValidation.setEmptyCellAllowed(true);
            dataValidation.setSuppressDropDownArrow(true);
            dataValidation.setShowPromptBox(true);
            dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP);
    
            sheet.addValidationData(dataValidation);
        }

    二、其他

    2.1、列筛选

    查看excel实现

      

    POI代码  

    Sheet sheetCreat = wbCreat.createSheet(sheet.getSheetName());
    CellRangeAddress c = CellRangeAddress.valueOf(CELL_RANGE_ADDRESS);
    sheetCreat.setAutoFilter(c);
  • 相关阅读:
    SQL 初级教程学习(二)
    QuartzJobs 如何发布服务
    Net 发布网站中遇到的几点问题
    PWBI--Excel 数据源
    微信小程序资源
    加密和解密之非对称加密
    Js 使用小技巧总结(1)
    路径的读取
    json和Jsonp 使用总结(3)
    json和Jsonp 使用总结(2)
  • 原文地址:https://www.cnblogs.com/bjlhx/p/11240696.html
Copyright © 2020-2023  润新知