注解类:

@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. JDK源码看ArrayList和Vector的一些区别

    最近在看JDK源码,从源码的角度记录一下ArrayList和Vector的一些区别 1.new a.不指定长度 Vector默认创建10个元素的数组 public Vector() { this(10 ...

  2. Codeforces1099D.Sum in the tree(贪心)

    题目链接:传送门 思路: 一个节点放的数越大,那么以它为根的子树的节点权值之和就越小. 所以我们要在合法的范围内,使偶数层节点的权值尽可能地大.也就是说,令它的权值是子节点的最小值,这样保证了它的子节 ...

  3. Java基于opencv实现图像数字识别(五)—投影法分割字符

    Java基于opencv实现图像数字识别(五)-投影法分割字符 水平投影法 1.水平投影法就是先用一个数组统计出图像每行黑色像素点的个数(二值化的图像): 2.选出一个最优的阀值,根据比这个阀值大或小 ...

  4. hdu 1372 BFS

    A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...

  5. AOP统一处理Web请求日志

    <!--aop--> <dependency> <groupId>org.springframework.boot</groupId> <arti ...

  6. Javascript 4.4

    childNodes属性:可以从给定文档的节点树里把任何一个元素的所有子元素检索出来 返回的值是一个数组,此数组包含给定元素节点的全体子元素:element.childNodes nodeType属性 ...

  7. Pagedown learning notes

    Pagedown Links Google wiki page Download Markdown.Converter.js var converter = new Markdown.Converte ...

  8. mac下 改变了ssh连接的端口 git怎么修改

    1. 情况是这样的,为了安全起见,我们的服务器ssh连接端口改为了33 sudo vi /etc/ssh/ssh_config port  33 2. 因为git是基于ssh发送数据的,并且git服务 ...

  9. 使用python读取MS-SQL数据库

    使用python读取MS-SQL中的数据,这里使用到模板pymssql. 因为不是python自带的模板,所以首先需要使用pip安装,对应命令:pip install pymssql 建立main.p ...

  10. transmission跳过文件校验功能实现

    贴吧链接:https://tieba.baidu.com/p/4839039900 GitHub:https://github.com/superlukia/transmission-2.92_ski ...