教研工具类
:
package com.cmbchina.ams.cloud.oms.common.util;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.cmbchina.ams.cloud.oms.common.validation.*;
import org.apache.commons.lang3.StringUtils;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.regex.Pattern.compile;
/**
-
Excel导入字段校验
- @author IT007160 徐帆
/
public class ExcelImportValid {
/*-
Excel导入字段校验
-
@param object 校验的JavaBean 其属性须有自定义注解
-
@author IT007160 徐帆
/
public static void valid(Object object, AnalysisContext context) throws RuntimeException, UnsupportedEncodingException {
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
//设置可访问
field.setAccessible(true);
//属性的值
Object fieldValue = null;
try {
fieldValue = field.get(object);
} catch (IllegalAccessException e) {
throw new RuntimeException(String.format(“第%s行%s列-”+field.getAnnotation(ExcelValid.class).message()+“有误,请核实”, context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1));
}
//是否包含必填校验注解
boolean isExcelValid = field.isAnnotationPresent(ExcelValid.class);
if (isExcelValid && Objects.isNull(fieldValue)) {
throw new RuntimeException( String.format(“第%s行%s列-”+field.getAnnotation(ExcelValid.class).message()+",请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1));
}
//utf-8长度校验注解
boolean isExcelUtfValid = field.isAnnotationPresent(ExcelUtfValid.class);
if (isExcelUtfValid && Objects.nonNull(fieldValue)) {
String cellStr = fieldValue.toString();
int strLength = cellStr.getBytes(“UTF-8”).length;
int length = field.getAnnotation(ExcelUtfValid.class).max();
if (StringUtils.isNotBlank(cellStr) && strLength > length) {
throw new RuntimeException( String.format(“第%s行%s列-”+field.getAnnotation(ExcelUtfValid.class).message()+",请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1));
}
}
//字符串中是否有汉字校验
boolean isExcelChineseValid = field.isAnnotationPresent(ExcelChineseValid.class);
if (isExcelChineseValid && Objects.nonNull(fieldValue)) {
String cellStr = fieldValue.toString();
Pattern p = compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(cellStr);
if (m.find()) {
throw new RuntimeException( String.format(“第%s行%s列-”+field.getAnnotation(ExcelChineseValid.class).message()+",请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1));
}
}
//特殊字符校验
boolean isExcelSpecialCharValid = field.isAnnotationPresent(ExcelSpecialCharValid.class);
if (isExcelSpecialCharValid && Objects.nonNull(fieldValue)) {
String cellStr = fieldValue.toString();
Pattern p = compile("1+KaTeX parse error: Expected 'EOF', got '}' at position 317: … } }̲ //GBK长度校验…");
if (pattern.matcher(cellDecimal.toString()).matches()) {
flag = true;
}/
int i =cellDecimal.compareTo(min);if ( cellDecimal.compareTo(min) <= 0 || cellDecimal.compareTo(max) >= 0 ) { throw new RuntimeException(String.format("第%s行%s列-"+field.getAnnotation(ExcelDecimalValid.class).message()+",请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1) ); } BigDecimal bigDecimal = new BigDecimal(0.01); int a = cellDecimal.compareTo(new BigDecimal("0.01")); int b = cellDecimal.compareTo(bigDecimal); if ( cellDecimal.compareTo(new BigDecimal("0.01")) < 0 ) { throw new RuntimeException(String.format("第%s行%s列-"+field.getAnnotation(ExcelDecimalValid.class).message()+",请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1) ); } } else { throw new RuntimeException(String.format("第%s行%s列-"+field.getAnnotation(ExcelDecimalValid.class).message()+",不是小数数字类型请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1)); } }
}
}
private static boolean isBigDecimal(String decimal) {
try {
BigDecimal bd = new BigDecimal(decimal);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
-
\u4E00-\u9FA5A-Za-z0-9 ↩︎