上传excel数据校验表头名称和顺序正确性工具类

public class ExcelUtil {

    /**
     * 校验excel表头 顺序及表头内容
     *
     * @param in
     * @param clazz
     * @return 表头异常信息提示
     * @throws IOException
     */
    public static String verifyExcelHeader(InputStream in, Class clazz) throws IOException {
        String result;
        try {
            Workbook wb = new XSSFWorkbook(in);
            result = ExcelUtil.checkExcelHeadLine(wb, clazz);
        } catch (Exception e){
            throw new BusinessException("verify excel header analysis is fail");
        } finally {
            if (null != in) {
                in.close();
            }
        }
        return result;
    }

    /**
     * 校验上传excel表头信息
     *
     * @param wb
     * @param clazz
     * @return
     */
    public static String checkExcelHeadLine(Workbook wb, Class clazz) throws NoSuchFieldException {
        String result = null;
        //通过class获取到使用@ExcelProperty注解配置的字段
        Map<Integer, String> head = getIndexNameMap(clazz);
        //获取第一个数据sheet页
        Sheet sheet = wb.getSheetAt(0);
        //获取sheet页第一行数据
        Row row = sheet.getRow(0);
        if (null != row && row.getLastCellNum() >= head.size()) {
            for (int idx = 0; idx < row.getLastCellNum(); idx++) {
                if (idx < head.size()) {
                    //获取表头内容
                    String value = row.getCell(idx).getStringCellValue();
                    if (StringUtils.isBlank(value) || !head.get(idx).equals(value)) {
                        result = String.format("上传excel表头行第%d列名称错误!", (idx + 1));
                        return result;
                    }
                }
            }
        } else {
            result = "上传excel表头与模板表头长度不一致!";
            return result;
        }
        return null;
    }

    /**
     * @param clazz
     * @return
     * @throws NoSuchFieldException
     */
    public static Map<Integer, String> getIndexNameMap(Class clazz) throws NoSuchFieldException {
        Map<Integer, String> result = new HashMap<>();
        Field field;
        //获取类中所有的属性
        Field[] fields = clazz.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            field = clazz.getDeclaredField(fields[i].getName());
            field.setAccessible(true);
            //获取根据注解的方式获取ExcelProperty修饰的字段
            ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
            if (excelProperty != null) {
                //索引值
                int index = excelProperty.index();
                //字段值
                String[] values = excelProperty.value();
                StringBuilder value = new StringBuilder();
                for (String v : values) {
                    value.append(v);
                }
                result.put(index, value.toString());
            }
        }
        return result;
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值