poi 对日期cell的验证转换

Excel 文件中有的人在日期的单元格里面输入字符串,有的人输入日期类型的数据,这就比较讨厌

所以读取日期的单元格要小心

下面是一个方法,验证上传excel文件时输入的日期是否正确的。

public static boolean checkFilebyDate(File file,Date fromDate,Date toDate){
		SimpleDateFormat  parseTime = new SimpleDateFormat("dd/MM/yyyy");
		FileInputStream in = null;
		try {
			in = new FileInputStream(file);
			HSSFWorkbook workbook = new HSSFWorkbook(in); 
			HSSFSheet sheet = workbook.getSheetAt(0);   
			int lrnum = sheet.getLastRowNum();  
			Date excelFromDate = null;
			Date excelToDate = null;
			HSSFCell cellTo = sheet.getRow(1).getCell(1);
			HSSFCell cellFrom = sheet.getRow(lrnum-1).getCell(1);
			if(cellTo.getCellType() == HSSFCell.CELL_TYPE_STRING){
				String from = cellFrom.getStringCellValue();
				String to = cellTo.getStringCellValue();
				excelFromDate = parseTime.parse(from);
				excelToDate = parseTime.parse(to);
			}else if(cellTo.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){			
				double from = cellFrom.getNumericCellValue();
				double to = cellTo.getNumericCellValue();
				//boolean b = HSSFDateUtil.isCellDateFormatted(cellTo);
				excelFromDate = HSSFDateUtil.getJavaDate(from);
				excelToDate = HSSFDateUtil.getJavaDate(to);
			}else{
				return false;
			}
			
			in.close();
			if(excelFromDate.getTime() < fromDate.getTime()){
				return false;
			}else if(excelToDate.getTime() > toDate.getTime()){
				return false;
			}else{
				return true;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} catch (ParseException e) {
			e.printStackTrace();
			return false;
		}finally{
			System.out.print("checkFilebyDate");
		}			
	}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值