注解 - Excel 校验工具
注解类:
@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 校验工具的更多相关文章
- Excel解析工具easyexcel全面探索
1. Excel解析工具easyexcel全面探索 1.1. 简介 之前我们想到Excel解析一般是使用POI,但POI存在一个严重的问题,就是非常消耗内存.所以阿里人员对它进行了重写从而诞生了eas ...
- 使用Apache poi来编写导出excel的工具类
在JavaWeb开发的需求中,我们会经常看到导出excel的功能需求,然后java并没有提供操作office文档的功能,这个时候我们就需要使用额外的组件来帮助我们完成这项功能了. 很高兴Apache基 ...
- Manifesto – HTML5 离线应用程序缓存校验工具
Manifesto 是一个 HTML5 离线应用程序缓存校验工具,提供了快速校验 HTML5 manifest 文件有效性的方法.离线应用程序缓存在使用中最困难的部分之一就是无法正常工作的时候没有明显 ...
- ESLint – 可扩展的 JavaScript & JSX 校验工具
ESLint 是一个开源的 JavaScript 代码校验工具,最初是由 Nicholas C. Zakas 在2013年创建的.经常被用来发现问题的模式或代码,不符合特定的风格准则. ESLint ...
- 基于jdk1.7实现的excel导出工具类
通用excel导出工具类,基于泛型.反射.hashmap 以及基于泛型.反射.bean两种方式 import java.io.*;import java.lang.reflect.Field;impo ...
- excel读取 工具类
package cn.yongche.utils; import java.io.File; import java.io.FileInputStream; import java.io.IOExce ...
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...
- Java对象和Excel转换工具XXL-EXCEL
<Java对象和Excel转换工具XXL-EXCEL> 一.简介 1.1 概述 XXL-EXCEL 是一个灵活的Java对象和Excel文档相互转换的工具. 一行代码完成Java对象和Ex ...
- Excel导入工具类兼容xls和xlsx
package com.bj58.finance.platform.operation.provider.util; import org.apache.log4j.Logger; import or ...
随机推荐
- KeyguardSliceView.java
/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Versi ...
- org.springframework.orm.hibernate3.HibernateSystemException:
org.springframework.orm.hibernate3.HibernateSystemException: The database returned no natively gener ...
- docker安装mysql容器后,是用navicat连接报client does not support authentication protocol requested by server consider upgrading mysql client
#进入容器 docker exec -it mysql bash#进入mysqlmysql -u root -p#重置密码ALTER USER 'root'@'%' IDENTIFIED WITH m ...
- Mysql update指定区间的数据
,) as a) Mysql 中 limit不能作为字句,所以要在limit外面再嵌套一层select
- jq ajax数据交互
get 与 post 的区别 了解和使用 get和post是HTTP与服务器交互的方式, 说到方式,其实总共有四种:put,delete,post,get. 他们的作用分别是对服务器资源的增,删,改, ...
- 锋利的jQuery初学(3)
jQuery详细介绍 1,$的含义:就是一个名称符号:jquery占用了两个变量:$和jquery; 2,js与jQuery的入口函数区别 (1),js的window.onload事件是等到所有内容加 ...
- zabbix监控Apache
zabbix 3.4.4监控apache 一.Apache开启状态页面 [root@oneapm-test scripts]# httpd -v Server version: Apache/2. ...
- postgresql 最大连接数相关
PG中有一张表记录着当前有多少连接 表名:pg_stat_activity 查询当前连接数: select count(1) from pg_stat_activity; 查询最大连接数 show m ...
- 谈一谈socket与java
用java中socket实现socket聊天 1, 什么是socket Socket 是指网络套接字,什么是套接字呢? 这是网络上一种端对端的网络协议,端口就是进程号,socket在网 ...
- Java_03选择结构
1.if 选择结构 格式: if(判断条件){ // 语句块 }else if(判断条件){ // 语句块 2 }else{ // 语句块 3 } 当 if 关键字后的一对大括号里只有一个语句时,可以 ...