一、数据验证概述
推荐以下操作在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);
原文地址:https://www.cnblogs.com/bjlhx/p/11240696.html
时间: 2024-10-29 19:06:58