java使用poi,做一个通用的导入导出,核对数据格式的要求,对数据格式长度和对日期的要求验证

书接上文!!!
https://blog.csdn.net/drsbbbl/article/details/102742847
对上次更加优化,添加了对应的数据的校验和对日期的校验。
和导出数据的小数点位数。
新的 单元格注解类



package com.fulan.server.common.anno;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.fulan.server.common.excelenmu.ColumnType;
import com.fulan.server.common.excelenmu.ColumnValiDataIsNull;

@Target(value = { ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExcelColumn {
	/**
	 * 导出的 列的名字
	 * 
	 * @return
	 */
	String columnName() default "test";

	/**
	 * 对应的 列的下标
	 * 
	 * @return
	 */
	int columnIndex();

	/**
	 * 日期的类型
	 * 
	 * @return
	 */
	ColumnType columnType() default ColumnType.notDateType;

	/**
	 * 是否验证数据 是不是可以为空
	 */
	ColumnValiDataIsNull valiData() default ColumnValiDataIsNull.CanEmpty;

	/**
	 * 导出数据验证长度
	 */
	int dataLength() default 0;

	/**
	 * 导出数据 小数点位数
	 * 
	 * @return
	 */
	String decimalPoint() default "";
}


对应的日期验证方法:



public static boolean checkDate(String str, String pattern) {
		SimpleDateFormat sd = new SimpleDateFormat(pattern);
		try {
		//注意此处必须设定 不然 不正确的日期 Java也会兼容
		// 比如 2018-13-01  
		//他会 替换为 2019-01-01
			sd.setLenient(false);// 此处指定日期/时间解析是否不严格,在true是不严格,false时为严格
			sd.parse(str);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

对应的代码处理中,添加两个map 做对应的处理存储类型存储对应的注解值

/**
			 * 数据校验
			 */
			Map<Integer, ColumnValiDataIsNull> dataValiedMap = Maps.newHashMap();

			/**
			 * 导出数据验证长度
			 */
			Map<Integer, Integer> dataLength = Maps.newHashMap();
			/**
			 * 导出数据保留 小数点位数
			 * 
			 * @return
			 */
			Map<Integer, String> decimalPoint = Maps.newHashMap();

使用map来存储对应的结果:

Map<String, Object> mapResutl = Maps.newHashMap();
		List<E> result = Lists.newArrayList();
		mapResutl.put("result", result);
		mapResutl.put("dataValiedMap", Lists.newArrayList());
		mapResutl.put("dataLength", Lists.newArrayList());
		mapResutl.put("dateMap", Lists.newArrayList());
		mapResutl.put("flag", 0);
		mapResutl.put("success", 0);

处理数据的过程中,有很大的变化: 添加了对应的不同的结果的处理过程
对数据校验做了判断!!!

ColumnType columnType = dateMap.get(index);
		boolean dateflag = true;
		if (columnType != null) {
			if (columnType.getKey() == 2) {
				dateflag = false;
			}
		}
		boolean flag = false;
		if (dataValiedMap.get(index).getKey() == 1) {
			// 需要验证
			flag = true;
		}
		boolean dataLengthflag = true;
		if (dataLength.get(index) == 0) {
			// 需要验证
			dataLengthflag = false;
		}
		boolean decimalflag = true;
		if ("".equals(decimalPoint.get(index))) {
			decimalflag = false;
		}
		DecimalFormat df = new DecimalFormat("#");
		if (flag) {
			if (cell == null || cell.getCellTypeEnum() == CellType.BLANK) {
				List<String> List = (java.util.List<String>) resultMap.get("dataValiedMap");
				List.add("第" + (rowIndex + 1) + "行中的,第" + (index + 1) + "列,数据为空");
				return null;
			}

		}
		if (cell == null) {
			return "标记";
		}
		if (cell.getCellTypeEnum() == CellType.STRING) {
			if (cell.getStringCellValue().length() == 0) {
				List<String> List = (java.util.List<String>) resultMap.get("dataValiedMap");
				List.add("第" + (rowIndex + 1) + "行中的,第" + (index + 1) + "列,数据为空");
				return null;
			}
			if (dataLengthflag) {
				String stringCellValue = cell.getStringCellValue();
				if (dataLength.get(index) == stringCellValue.length()) {
					return stringCellValue;
				}
				List List = (java.util.List) resultMap.get("dataLength");
				List.add("第" + (rowIndex + 1) + "行" + "中的,第" + (index + 1) + "列,数据长度应该为" + dataLength.get(index));
				return null;
			}
			if (dateflag) {
				// 需要验证日期
				String dateData = cell.getStringCellValue();
				if (!StrUtil.checkDate(dateData, columnType.getValue())) {
					List<String> List = (java.util.List<String>) resultMap.get("dateMap");
					List.add("第" + (rowIndex + 1) + "行中的,第" + (index + 1) + "列,日期的格式应该为!!!" + columnType.getValue());
					return null;
				}
			}
			return cell.getStringCellValue();
		} else if (cell.getCellTypeEnum() == CellType.BOOLEAN) {
			return String.valueOf(cell.getBooleanCellValue());
		} else if (cell.getCellTypeEnum() == CellType.NUMERIC) {
			if (HSSFDateUtil.isCellDateFormatted(cell)) { // 判断是日期类型
				Date dt = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());// 获取成DATE类型
				SimpleDateFormat sim = new SimpleDateFormat(columnType.getValue());

				return sim.format(dt);
			} else { // 转化电话号码和身份证号码为字符串
				if (decimalflag) {
					double test = cell.getNumericCellValue();
					String sb = "#.";
					for (int i = 0; i < Integer.parseInt(decimalPoint.get(index)); i++) {
						sb += "0";
					}
					DecimalFormat tem = new DecimalFormat(sb);
					return Double.parseDouble(tem.format(test));
				}
				return String.valueOf(df.format(cell.getNumericCellValue()));
			}
		}
		return null;
	if (cellValue == null) {
					mapResutl.put("flag", (Integer.parseInt(mapResutl.get("flag").toString()) + 1));
					flag = true;
					break;
				}
				if ("标记".equals(cellValue)) {
					setObjectToAtrrbute(newInstance, declaredField);
					continue;
				}

	private static void setObjectToAtrrbute(Object obj, Field field) throws Exception {
		String fieldType = field.getType().getSimpleName();
		if ("String".equals(fieldType)) {
			field.set(obj, "");
			if ("dateGuarantee".equals(field.getName())) {
				Method declaredMethod = obj.getClass().getDeclaredMethod("getInitialDate", null);
				declaredMethod.setAccessible(true);
				Object invoke = declaredMethod.invoke(obj, null);
				SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
				// 初等日期
				Date parse = sim.parse(invoke.toString());
				Calendar cal = Calendar.getInstance();
				cal.setTime(parse);
				cal.add(Calendar.DAY_OF_YEAR, 1);
				Date time = cal.getTime();
				field.set(obj, sim.format(time));
			}
		} else if ("Date".equals(fieldType)) {
			Method declaredMethod = obj.getClass().getDeclaredMethod("getInitialDate", null);
			declaredMethod.setAccessible(true);
			Object invoke = declaredMethod.invoke(obj, null);
			SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
			// 初等日期
			Date parse = sim.parse(invoke.toString());
			Calendar cal = Calendar.getInstance();
			cal.setTime(parse);
			cal.add(Calendar.DAY_OF_YEAR, 1);
			Date time = cal.getTime();
			System.out.println(time);
			field.set(obj, sim.format(time));
		} else if ("Integer".equals(fieldType) || "int".equals(fieldType)) {
			field.set(obj, 0);
		} else if ("Long".equalsIgnoreCase(fieldType)) {
			field.set(obj, 0);
		} else if ("Double".equalsIgnoreCase(fieldType)) {
			field.set(obj, 0);
		}

	}

同理对应的代码我也放到对应的git上 请大佬指点!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值