EasyPoi 导入Excel获取不到第一列的问题(解决)

这里我举个例子(我遇到的问题)
我的Excel模板是这样的
在这里插入图片描述
但是测试好多次 扫描不到第一列(产品唯一标识)的数据
获取到的结果
在这里插入图片描述

第一列标识的modelId 一直是null

错误的代码

 @PostMapping("upload")
    @Transactional
    public Response upload(CustomerPrice customerPrice,@RequestParam("file") MultipartFile multipartFile) throws Exception {
        try {
            ImportParams params = new ImportParams();
            //标题和表头不能混合起来 用表头 = 2来代替标题  这样会导致第一列读取不到
            params.setHeadRows(2);
//            params.setTitleRows(1);

            List<CustomerPrice> result = ExcelImportUtil.importExcel(multipartFile.getInputStream(),
                    CustomerPrice.class, params);
            System.out.println(result.toString());
            CustomerPrice customerPrice1 = new CustomerPrice();
            //第一步 先删除
            customerPriceService.deleteByCompany(customerPrice.getMerchantCompanyId(),customerPrice.getCustomerCompanyId());
            result.forEach(item->{
                //第二步 添加公司id
                item.setCustomerCompanyId(customerPrice.getCustomerCompanyId());
                item.setMerchantCompanyId(customerPrice.getMerchantCompanyId());
                System.out.println(item.toString());
            });
            //第三步 将改造后的list放入对象 执行添加
            customerPrice1.setCustomerPrices(result);
            customerPriceService.insert(customerPrice1);
            return ResponseFactory.bool().message("导入成功!");
        } catch (Exception e){
            e.printStackTrace();
            throw new BizException("导入失败,请检查参数!");
       }

    }

乍一看 没啥错误的
但是主要的问题就是在于这个

  params.setHeadRows(2);

我这个Excel 表是 标题一行 表头一行

但是我设置了2

很多人和我同样的错误 (因为代码是我直接copy的)

直接用表头将标题也包含进去了,这就会导致这个扫描不到第一列的问题

正确的设置应该是

 params.setHeadRows(1);//表头
 params.setTitleRows(1);//标题

很多人对表头和标题分不清楚

在这里插入图片描述
所以你在有标题的时候,titleRows设置1,如果没有的话,设置0或者不设置(默认是0)

这样问题就解决了
在这里插入图片描述

  • 22
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值