注解类:

@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelValidate
{
public boolean ignoreBlank() default false; public int max() default Integer.MAX_VALUE; public int min() default 0; public boolean notNull() default false; public String format() default "";
}

被注解类:

public class ExcelAssetsBusinessVO {

    @ExcelValidate(notNull=true,max=100)
private String name; @ExcelValidate(notNull=true)
private String ip; private String port;
}

注解解析工具:

public class ExcelValidateUtil {
public static <E> List<String> check(List<E> list, int index) throws Exception {
List<String> strList = new ArrayList<>();
Iterator<E> it = list.iterator();
int i = 0;
while(it.hasNext()) {
int offset = i + index;
E e = it.next();
Field[] fields = e.getClass().getDeclaredFields(); //对excel中的空行进行校验
int count = 0, step = 0;
ExcelValidate excelValidateClass = e.getClass().getAnnotation(ExcelValidate.class);
if (excelValidateClass.ignoreBlank()) {
for (Field field : fields) {
count++;
field.setAccessible(true);
if (field.get(e) == null || StringUtils.isBlank(field.get(e).toString())) {
step++;
}
}
}
if (count == step) {
it.remove();
continue;
} for (Field field : fields) {
field.setAccessible(true);
if (field.isAnnotationPresent(ExcelValidate.class)) {
ExcelValidate excelValidate = field.getAnnotation(ExcelValidate.class);
ExcelField excelField = field.getAnnotation(ExcelField.class);
if (excelValidate.notNull()) {
if (field.get(e) == null || field.get(e).toString().trim().length() == 0) {
String str = "第" + offset + "行," + excelField.title().substring(0, excelField.title().length()-1) + "不能为空!";
strList.add(str);
continue;
}
}
if (excelValidate.max() != Integer.MAX_VALUE) {
if (field.get(e)!=null && field.get(e).toString().length() > excelValidate.max()) {
String str = "";
if(excelField.title().endsWith("*")) {
str = "第" + offset + "行," + excelField.title().substring(0, excelField.title().length()-1) + "不能超过" + excelValidate.max() + "字符!";
}else {
str = "第" + offset + "行," + excelField.title() + "不能超过" + excelValidate.max() + "字符!";
}
strList.add(str);
continue;
}
}
if (excelValidate.min() != 0) {
if (field.get(e)!=null && field.get(e).toString().length() < excelValidate.min()) {
String str = "";
if(excelField.title().endsWith("*")) {
str = "第" + offset + "行," + excelField.title().substring(0, excelField.title().length()-1) + "不能少于" + excelValidate.max() + "字符!";
}else {
str = "第" + offset + "行," + excelField.title() + "不能少于" + excelValidate.max() + "字符!";
}
strList.add(str);
continue;
}
}
if (field.get(e)!=null && !"".equals(excelValidate.format())) {
boolean valid = Pattern.matches(excelValidate.format(), field.get(e).toString()); if (!valid) {
String str = "";
if(excelField.title().endsWith("*")) {
str = "第" + offset + "行," + excelField.title().substring(0, excelField.title().length()-1) + "格式不正确!";
} else {
str = "第" + offset + "行," + excelField.title() + "格式不正确!";
}
strList.add(str);
continue;
}
}
}
}
i++;
}
return strList;
}
}

注解 - Excel 校验工具的更多相关文章

  1. Excel解析工具easyexcel全面探索

    1. Excel解析工具easyexcel全面探索 1.1. 简介 之前我们想到Excel解析一般是使用POI,但POI存在一个严重的问题,就是非常消耗内存.所以阿里人员对它进行了重写从而诞生了eas ...

  2. 使用Apache poi来编写导出excel的工具类

    在JavaWeb开发的需求中,我们会经常看到导出excel的功能需求,然后java并没有提供操作office文档的功能,这个时候我们就需要使用额外的组件来帮助我们完成这项功能了. 很高兴Apache基 ...

  3. Manifesto – HTML5 离线应用程序缓存校验工具

    Manifesto 是一个 HTML5 离线应用程序缓存校验工具,提供了快速校验 HTML5 manifest 文件有效性的方法.离线应用程序缓存在使用中最困难的部分之一就是无法正常工作的时候没有明显 ...

  4. ESLint – 可扩展的 JavaScript & JSX 校验工具

    ESLint 是一个开源的 JavaScript 代码校验工具,最初是由 Nicholas C. Zakas 在2013年创建的.经常被用来发现问题的模式或代码,不符合特定的风格准则. ESLint ...

  5. 基于jdk1.7实现的excel导出工具类

    通用excel导出工具类,基于泛型.反射.hashmap 以及基于泛型.反射.bean两种方式 import java.io.*;import java.lang.reflect.Field;impo ...

  6. excel读取 工具类

    package cn.yongche.utils; import java.io.File; import java.io.FileInputStream; import java.io.IOExce ...

  7. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  8. Java对象和Excel转换工具XXL-EXCEL

    <Java对象和Excel转换工具XXL-EXCEL> 一.简介 1.1 概述 XXL-EXCEL 是一个灵活的Java对象和Excel文档相互转换的工具. 一行代码完成Java对象和Ex ...

  9. Excel导入工具类兼容xls和xlsx

    package com.bj58.finance.platform.operation.provider.util; import org.apache.log4j.Logger; import or ...

随机推荐

  1. jQuery-3.事件篇---键盘事件

    jQuery键盘事件之keydown()与keyup()事件 鼠标有mousedown,mouseup之类的事件,这是根据人的手势动作分解的2个触发行为.相对应的键盘也有这类事件,将用户行为分解成2个 ...

  2. 2018-2019-2 网络对抗技术 20165228 Exp6 信息搜集与漏洞扫描

    2018-2019-2 网络对抗技术 20165228 Exp6 信息搜集与漏洞扫描 回答问题 哪些组织负责DNS,IP的管理. 全球根服务器均由美国政府授权的ICANN统一管理,负责全球的域名根服务 ...

  3. vue—data中变量和字符串拼接

    #变量和字符串的拼接# 写项目中,遇到了这样的一个问题:怎样在一个div里面显示两个data中的数据?我的问题描述清楚了吗?... 看图吧:   这是用户最初的需求~  这是用户后来的需求,嗯……就是 ...

  4. SQLI DUMB SERIES-14

    (1)闭合方式为一对双引号 (2)可用报错注入.如: admin" and extractvalue(1,concat(0x7e,(select database()))) and &quo ...

  5. MySQL 对分组后的同类数据进行拼接字符串

    MySQL 对分组后的同类数据进行拼接字符串 写后台方法时遇到个问题,需要将表内同一订单号的操作记录流水进行简单拼接输出,不想取出来再操作,找了个mysql的方法直接操作 //group_concat ...

  6. bootstrap-daterangepicker双日历控件开始日期选择问题

    在做项目的时候,有这样一个需求,需要选择时间段,于是在网上找了bootstrap的双日历时间控件daterangepicker控件,要选取时间段的年月来与后台进行数据交互,时间控件如下图所示: 关于控 ...

  7. javascript将C#json序列化的Date对象转换成正常的Date格式

    function ChangeDateFormat(cellval) { var date = new Date(parseInt(cellval.replace("/Date(" ...

  8. 使用multidex解决64K方法引用的限制

    1.什么是64K方法引用的限制 65536(64K)是单个dex(Dalvik Executable)字节码文件的可引用的方法数的最大数,包括Android framework.应用的library和 ...

  9. el-popover 的显示或隐藏,要在拿到真实dom之后再做控制

    el-popover 的显示或隐藏,要在拿到真实dom之后再做控制

  10. python-简单的登陆接口

    # 工作需求2 编写登陆接口(1输入用户名密码 2认证成功后显示欢迎信息 3输错三次后锁定)# 实现思路 1.用户名和密码是否正确,全部正确可正常登陆 输错三次无法登陆 2.是否已经注册 未注册 无法 ...