引用jar包
<!--excel导入导出-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.6</version>
</dependency>
自定义注解
package com.jxcc.trade.annotation;
import java.lang.annotation.*;
/**
* excel 校验
* @author zhichong
*/
@Documented
@Inherited
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE}) //可以在字段、枚举的常量、方法
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelCheck {
//不能为空
boolean isNotNull() default true;
//整数长度
int integer() default -1;
//小数长度
int decimal() default -1;
//时间格式
String dateFormat() default "";
//正则校验
String matches() default "";
}
校验工具类
package com.jxcc.trade.utils;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.jxcc.trade.annotation.ExcelCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* excel 工具类
* @author zc
*/
public class ExcelUtils {
private static Logger log = LoggerFactory.getLogger(ExcelUtils.class);
/**
* java对象转map
* @param obj
* @return
*/
public static Map<?, ?> objectToMap(Object obj) {
Map userMap = new HashMap();
if (obj != null) {
userMap = JSON.parseObject(JSON.toJSONString(obj), new TypeReference<Map<String, String>>() {
});
}
return userMap;
}
/**
* java对象转map
* @param objList
* @return
*/
public static List<Map<String, Object>> objectToMapList(List objList) {
List<Map<String, Object>> objMapList = new ArrayList<>();
if (ObjectUtil.isNotEmpty(objList)) {
objMapList = JSONArray.parseObject(JSONArray.parseArray(JSON.toJSONString(objList)).toJSONString(), List.class);
}
return objMapList;
}
public static List<String> excelCheck(List excelList, Class<?> clazz) {
Map<String, ExcelCheck> excelCheckMap = new HashMap<String, ExcelCheck>();
Map<String, ExcelProperty> excelPropertyMap = new HashMap<String, ExcelProperty>();
for (Field field : TableInfoHelper.getAllFields(clazz)) {
ExcelCheck excelCheck = field.getAnnotation(ExcelCheck.class);
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
if (excelCheck != null) {
excelCheckMap.put(field.getName(), excelCheck);
excelPropertyMap.put(field.getName(), excelProperty);
}
}
List<String> errList = new ArrayList<>();
List<Map<String, Object>> objMap = objectToMapList(excelList);
for (Map<String, Object> map : objMap) {
for (Map.Entry<String, Object> data : map.entrySet()) {
String k = data.getKey();
Object v = data.getValue();
ExcelCheck excelCheck = excelCheckMap.get(k);
if (ObjectUtil.isNotEmpty(excelCheck)) {
// excelCheckAnnotation(excelPropertyMap.get(k),)
if (excelCheck.isNotNull()) {
if (ObjectUtil.isEmpty(v)) {
ExcelProperty excelProperty = excelPropertyMap.get(k);
String err = excelProperty.value() + "不能为空";
errList.add(err);
}
}
if (excelCheck.decimal() != -1) {
//TODO 先判断 是纯数字
int decimal = excelCheck.decimal();
}
}
}
}
//处理反射
return errList;
}
// public static void main(String[] args) {
// List<BillingStandardVersionExcel> excelList = new ArrayList();
// BillingStandardVersionExcel excel1 = new BillingStandardVersionExcel();
// excel1.setMainElementCode("2222");
// excelList.add(excel1);
// BillingStandardVersionExcel excel2 = new BillingStandardVersionExcel();
// excel2.setMainElementCode("22");
// excel2.setMainLowerLimit(new BigDecimal("33.666"));
// excelList.add(excel2);
//
JSONArray.parse()
//
List<Map> mapList = JSONArray.parseObject(JSONArray.parseArray(JSON.toJSONString(excelList)).toJSONString(), List.class);
JSONArray.parseArray(JSON.toJSONString(excelList));
// System.out.println("userMap");
//
// excelCheck(excelList, BillingStandardVersionExcel.class);
//
// }
}