注解类:

@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. java求最大值以及定义方法调用

    class ArrayDome { public static void main(String[] args) { int[] arr = {-12,-51,-12,-11}; int max = ...

  2. oc语言的Foundation框架(学习笔记2)

    紧接上文…… 4.集合对象 4.1数组 1.基本概念 Foundation中的数组(NSArray,NSMutableArray)是一组有序的对象集合,通过索引下标获取到数组中的各个元素,也分可变和不 ...

  3. Python中的变量作用域

    python中变量作用域包括: L (Local) 局部作用域,函数内部声明但没有使用global的变量E (Enclosing) 闭包函数外的函数中,def或者lambda的本地作用域G (Glob ...

  4. oracle数据库连接缓慢

    今天连接数据库时非常的慢,大概将近1分钟,才能连上,感觉不对,登录数据库所在的服务器,进行检查,常规的内存.硬盘.CPU全都正常,然后想要进入oracle用户时报错了:su: cannot set u ...

  5. windows10环境下VMware14中Ubuntu16.04解决如何上网问题

    进入控制面板,网络和Internet,网络连接 点击以太网,查看详细信息 点击属性 --> 共享,允许其他网络-->选择VMnet1 点开虚拟机编辑选项 ,选择 VMNET1 仅主机,其他 ...

  6. Python3常用函数、方法总结(持续更新…)

    最近刷LeetCode,自己自娱自乐完之后去discussion看大佬们的各种巧妙解法,总是止不住的双击666--加上最近Python3用的比较多(虽然Python实在不推荐跑算法题目,一是运行效率太 ...

  7. c# excel xls保存

    public HSSFWorkbook Excel_Export(DataTable query,string title,int[] rowweight,string[] rowtitle) { H ...

  8. Unity RigidBodyFPSController 鼠标不显示

    做第一人称浏览和顶视图浏览时遇到一个坑,就是当切换到第一人称时,操作UI界面的时候就gg,鼠标光标都看不见了. 如下图:LockCursor LockCursor 做了两个操作,第一个就是锁定光标位置 ...

  9. Spring-Cloud-Netflix

    Spring Cloud Netflix组件以及部署 (1)Eureka,服务注册和发现,它提供了一个服务注册中心.服务发现的客户端,还有一个方便的查看所有注册的服务的界面. 所有的服务使用Eurek ...

  10. c# 枚举安卓系统中所有目录及文件名

    using Android.App; using Android.Widget; using Android.OS; using System.Runtime.InteropServices; nam ...