码迷,mamicode.com
首页 > 其他好文 > 详细

002-poi-excel-导出设置单元格数据校验规则

时间:2019-07-24 21:01:44      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:ast   ddr   http   div   erb   unique   sele   概述   min   

一、数据验证概述

推荐以下操作在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);

 

002-poi-excel-导出设置单元格数据校验规则

标签:ast   ddr   http   div   erb   unique   sele   概述   min   

原文地址:https://www.cnblogs.com/bjlhx/p/11240696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!