easyexcel-导入校验规则设置

引用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);
//
//    }


}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EasyExcel 是一个用于操作 Excel 文件的 Java 库。要限定时间格式,你可以使用 EasyExcel 提供的样式功能。下面是一个示例代码,演示如何将时间格式设置为 "yyyy-MM-dd HH:mm:ss": ```java // 导入 EasyExcel 相关的包 import com.alibaba.excel.EasyExcel; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategyImpl; import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy; // 定义数据模型类 public class DataModel { @DateTimeFormat("yyyy-MM-dd HH:mm:ss") private Date time; // 省略其他属性和方法 } public class ExcelWriter { public static void main(String[] args) { // 创建 ExcelWriter 对象 ExcelWriter excelWriter = EasyExcel.write("output.xlsx", DataModel.class).build(); // 设置时间格式样式 WriteCellStyle dateCellStyle = new WriteCellStyle(); WriteFont dateFont = new WriteFont(); dateFont.setFontName("Arial"); dateFont.setFontHeightInPoints((short) 12); dateCellStyle.setWriteFont(dateFont); dateCellStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("yyyy-MM-dd HH:mm:ss")); // 获取 Sheet 对象并写入数据 WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build(); writeSheet.setColumnWidth(0, 20); // 设置列宽度 // 设置列样式策略,这里设置第一列的样式为时间格式样式 LongestMatchColumnWidthStyleStrategy styleStrategy = new LongestMatchColumnWidthStyleStrategyImpl(new SimpleColumnWidthStyleStrategy(), dateCellStyle); excelWriter.write(dataList, writeSheet, styleStrategy); // 关闭 ExcelWriter 对象 excelWriter.finish(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值