easyExcel导入踩坑记录

该篇文章讲述了如何使用Java方法处理上传的MultipartFile类型的Excel文件,对专利发明数据进行导入,包括根据类型(发明专利、实用新型、软著)设置类型字段,处理日期识别问题,并在遇到异常时进行适当的错误处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

字段均为字符串类型,其中两个字段解析不到数据,一直为空

字段类型:

 

核心代码

    @Override
    public String importFile(MultipartFile file, String nickName, Long userId) {
        List<PatentInvention> inventions = FileUtil.importExcel(file, 0, 1, PatentInvention.class);
        if (!CollectionUtils.isEmpty(inventions)) {
            for (PatentInvention invention : inventions) {
                if(StringUtils.equals("发明专利",invention.getTypeName())){
                    invention.setType(0);
                }else  if(StringUtils.equals("实用新型",invention.getTypeName())){
                    invention.setType(1);
                }if(StringUtils.equals("软著",invention.getTypeName())){
                    invention.setType(2);
                }
                invention.setCreateTime(new Date());
                invention.setCreateBy(nickName);
                patentInventionMapper.insert(invention);
            }
        } else {
            return "导出失败,参数为空";
        }
        return null;
    }
    /**
     * 导入
     * @param file 需要导入的文件
     * @param titleRows 从第一行开始解析
     * @param headerRows 头部行下标
     * @param pojoClass 转化为对应的实体类
     * @param <T>
     * @return 返回解析后的实体类对象集合
     */
    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
        if (file == null){
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        }catch (NoSuchElementException e){
            //throw new NotFoundException("excel文件不能为空");
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
           // throw new NotFoundException(e.getMessage());
        }
        return list;
    }

结果(数据不全)

问题解决

1.字段中文括号问题

2.excel中日期识别不到问题

接受字段为字符串类型,接收不到其中日期,部分能接收到

正常的日期展示

提示有空格的字符串日期

采用笨办法,将日期全部增加空格,调整格式,进行导入

导入结果(正常展示)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值