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

Posted bjlhx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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,02)=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-导出设置单元格数据校验规则的主要内容,如果未能解决你的问题,请参考以下文章

Poi导入校验因单元格格式产生的空对象问题

JAVA中导出Excel将全部单元格设置为文本样式,就是导出的没有数据的单元格类型的设置

JAVA POI 导出 EXCEL模板 怎么设置单元格格式为 文字

JAVA POI 导出 EXCEL模板 怎么设置单元格格式为 文字

如何设定PLSQL DEVELOPER导出的CSV文件中单元格格式为文本?

c#导出EXCEL设置单元格格式?