java判断excel格式_Java 设置Excel数据验证

import com.spire.xls.*;public classDataValidation {public static voidmain(String[] args) {//创建Workbook对象

Workbook workbook = newWorkbook();//获取第一个工作表

Worksheet sheet = workbook.getWorksheets().get(0);//在单元格B3中设置数字验证-仅允许输入1到100之间的数

sheet.getCellRange("B2").setText("请输入1-100之间的数:");

CellRange rangeNumber= sheet.getCellRange("B3");

rangeNumber.getDataValidation().setCompareOperator(ValidationComparisonOperator.Between);

rangeNumber.getDataValidation().setFormula1("1");

rangeNumber.getDataValidation().setFormula2("100");

rangeNumber.getDataValidation().setAllowType(CellDataType.Decimal);

rangeNumber.getDataValidation().setErrorMessage("Please input correct number!");

rangeNumber.getDataValidation().setShowError(true);

rangeNumber.getCellStyle().setKnownColor(ExcelColors.Color21);//在单元格B6中设置日期验证-仅允许输入1/1/1970到12/31/1970之间的日期

sheet.getCellRange("B5").setText("请输入1/1/1970-12/31/1970之间的日期:");

CellRange rangeDate= sheet.getCellRange("B6");

rangeDate.getDataValidation().setAllowType(CellDataType.Date);

rangeDate.getDataValidation().setCompareOperator(ValidationComparisonOperator.Between);

rangeDate.getDataValidation().setFormula1("1/1/1970");

rangeDate.getDataValidation().setFormula2("12/31/1970");

rangeDate.getDataValidation().setErrorMessage("Please input correct date!");

rangeDate.getDataValidation().setShowError(true);

rangeDate.getDataValidation().setAlertStyle(AlertStyleType.Warning);

rangeDate.getCellStyle().setKnownColor(ExcelColors.Color16);//在单元格B9设置字符长度验证-仅允许输入5个字符以内的文本

sheet.getCellRange("B8").setText("请输入不超过5个字符的文本:");

CellRange rangeTextLength= sheet.getCellRange("B9");

rangeTextLength.getDataValidation().setAllowType(CellDataType.TextLength);

rangeTextLength.getDataValidation().setCompareOperator(ValidationComparisonOperator.LessOrEqual);

rangeTextLength.getDataValidation().setFormula1("5");

rangeTextLength.getDataValidation().setErrorMessage("Enter a Valid String!");

rangeTextLength.getDataValidation().setShowError(true);

rangeTextLength.getDataValidation().setAlertStyle(AlertStyleType.Stop);

rangeTextLength.getCellStyle().setKnownColor(ExcelColors.Color14);//在单元格B12设置数字验证-仅允许输入大于等于18的整数

sheet.getCellRange("B11").setText("请输入大于等于18的整数:");

CellRange rangeinteger= sheet.getCellRange("B12");

rangeinteger.getDataValidation().setAllowType(CellDataType.Integer);

rangeinteger.getDataValidation().setCompareOperator(ValidationComparisonOperator.GreaterOrEqual);

rangeinteger.getDataValidation().setFormula1("18");

rangeinteger.getDataValidation().setErrorMessage("Enter a Valid String!");

rangeinteger.getDataValidation().setShowError(true);

rangeinteger.getDataValidation().setAlertStyle(AlertStyleType.Stop);

rangeinteger.getCellStyle().setKnownColor(ExcelColors.LightGreen1);//第二列自适应宽度

sheet.autoFitColumn(2);//保存文档

workbook.saveToFile("DataValidation.xlsx", ExcelVersion.Version2016);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/** * 此代码是完成从excel导入电话号码,将正确的电话号码保存到set集合中,因为set集合对于重复的值会覆盖,所以达到了去重复的值的用例,并累计了不正确的电话号码的个数,对电话号码进行了验证有效性。所需要的 dom4j-1.6.1.jar;geronimo-stax-api_1.0_spec-1.0.jar;poi-3.7-20101029.jar;poi-ooxml-3.7-20101029.jar;poi-ooxml-schemas-3.7-20101029.jar;xmlbeans-2.3.0.jar; */ public static void main(String[] args) { Long errorMobileTotal=0L; // 保存正确的电话号码 Set<String> mobileSet = new HashSet<String>(); try { XSSFWorkbook wb = new XSSFWorkbook("E:/workbook1.xlsx"); XSSFSheet sheet = wb.getSheetAt(0); XSSFRow row = null; XSSFCell cell = null; String mobileStr=""; for (int i = 0; i <= sheet.getLastRowNum(); i++) { row = sheet.getRow(i); //System.out.print("第" + i + "行共" + row.getLastCellNum() +"列: "); for (int y = 0; y < row.getLastCellNum(); y++) { cell = row.getCell(y); // 设置字段为字符类型 cell.setCellType(XSSFCell.CELL_TYPE_STRING); // 判断储存格的格式 if (cell != null) { // 取得单元格的值 mobileStr = cell.getStringCellValue(); // 对手机号码进行验证身份正确 if(isMobileNO(mobileStr)) { // 保存正确的手机号码 mobileSet.add(mobileStr); System.out.println("号码"+mobileStr+"正确"); } else { // 累计不正确的电话号码的个数 errorMobileTotal++; System.out.println("不正确的电话号码个数:"+errorMobileTotal); System.out.println("号码"+mobileStr+"不正确"); } } // end (cell != null) }// end 遍历当前行 } // end 遍历当前工作单元sheet System.out.println("总共的行数:"+ (Long.valueOf(sheet.getLastRowNum())+1)); } catch (Exception e) { e.printStackTrace(); } // 因为要去除重复的所以可能有存在替换的字符 System.out.println("不正确的电话号码个数:"+errorMobileTotal); System.out.println("正确的电话号码个数:" + mobileSet.size()); } public static boolean isMobileNO(String mobiles){ Pattern p = Pattern.compile("^(\\+86)*0*((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"); Matcher m = p.matcher(mobiles); return m.matches(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值