springboot +excel导入整理及采坑日志

最近因项目需要,业务需要用到excel批量导入的功能,之后就开始了采坑,哈哈哈

废话少说,直接上代码:

导入包:

<!--        Excel导入的jar包-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>

<!--       去掉以下jar,不然会报冲突 org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException-->
<!--        <dependency>-->
<!--            <groupId>org.apache.poi</groupId>-->
<!--            <artifactId>poi-ooxml-schemas</artifactId>-->
<!--            <version>3.14</version>-->
<!--        </dependency>-->

别问为何不用poi-ooxml-schemas,毕竟我是踩过坑了

@ResponseBody
@PostMapping("/uploadCardBatchNotExpressZone")
@ApiOperation(value = "上传批次不配送地区")
@WebLog(description = "上传批次不配送地区")
public ResponseResult<Object> uploadCardBatchNotExpressZone(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id) {
    if (file.isEmpty()) {
        log.info("上传的文件为空,请检查!");
        throw new BusinessException(ErrorEnum.ERROR_PARAM_EMPTY, "上传的文件为空,请检查!");
    }
    if (null == id) {
        log.info("批次组id不能为空");
        throw new BusinessException(ErrorEnum.ERROR_PARAM_EMPTY, "批次组id不能为空");
    }
    CardBatchDTO cardBatchDTO = cardBatchService.getCardBatchById(id);
    if (null == cardBatchDTO) {
        log.info("批次记录不存在");
        throw new BusinessException(ErrorEnum.ERROR_NOT_FOUNT, "批次记录不存在");
    }

    Integer uploadStatus = expressZoneService.uploadNotExpressZone(file, id, BusinessTypeEnum.CAR_BATCH.getCode(), DispatchTypeEnum.NOT_DISPATCH.getCode());
    return ResponseResultUtils.success("文件上传处理成功",uploadStatus);
}

服务层:

@Override
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class, RuntimeException.class})
    public Integer uploadNotExpressZone(MultipartFile file, Long businessId, Integer businessType, Integer dispatchType) throws BusinessException {
        String fileName = file.getOriginalFilename();
        log.info("上传的文件名为:" + fileName);
        if (StringUtils.isNotBlank(fileName)) {
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            if (!Constants.XLSX_SUFFIX.equals(suffixName) && !Constants.XLS_SUFFIX.equals(suffixName)) {
                log.info("所传的文件后缀不是Excel文件");
                throw new BusinessException(ErrorEnum.ERROR_PARAM_FORMAT, "所传的文件后缀不是Excel文件");
            }
        }
        List<ExpressZoneDTO> expressZoneDTOS = new ArrayList<>();
        try {
//            //根据路径获取这个操作excel的实例
//            XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
//            //根据页面index 获取sheet页
//            XSSFSheet sheet = wb.getSheetAt(0);
            // 兼容2007以前或以后的excel版本
            Workbook workbook = WorkbookFactory.create(file.getInputStream());
            Sheet hssfSheet = workbook.getSheetAt(0);  //示意访问sheet
            //实体类集合
//            XSSFRow row = null;
            //循环sesheet页中数据从第二行开始,第一行是标题
            for (int i = 1; i < hssfSheet.getPhysicalNumberOfRows(); i++) {
                //获取每一行数据
//                row = hssfSheet.getRow(i);
                Row row = hssfSheet.getRow(i);
                if (null != row) {
                    // 每行的第二列为编码名称
                    String stringCellValue = row.getCell(1).getStringCellValue();
                    // 每行的第三列为编码
                    String stringCellValue1 = row.getCell(2).getStringCellValue();
                    if (StringUtils.isNotBlank(stringCellValue) && StringUtils.isNotBlank(stringCellValue1)){
                        ExpressZoneDTO expressZoneDTO = new ExpressZoneDTO();
                        // 省市县拆分
                        String[] split = stringCellValue.split(" ");
                        expressZoneDTO.setProvince(split[0]);
                        expressZoneDTO.setCity(split[1]);
                        expressZoneDTO.setCounty(split[2]);
                        // 省市县拆分
                        String[] split1 = stringCellValue1.split(",");
                        expressZoneDTO.setProvinceCode(Integer.valueOf(split1[0]));
                        expressZoneDTO.setCityCode(Integer.valueOf(split1[1]));
                        expressZoneDTO.setCountyCode(Integer.valueOf(split1[2]));
                        expressZoneDTOS.add(expressZoneDTO);
                    }
                }
            }
        } catch (Exception e) {
            log.error("上传文件解析报错", e);
            throw new BusinessException(ErrorEnum.ERROR_BIZ_FAIL, "上传文件解析报错");
        }
        ExpressZoneInputDTO expressZoneInputDTO = new ExpressZoneInputDTO();
        expressZoneInputDTO.setBusinessId(businessId);
        expressZoneInputDTO.setBusinessType(businessType);
        expressZoneInputDTO.setDispatchType(dispatchType);
        expressZoneInputDTO.setExpressZoneDTOs(expressZoneDTOS);

        saveExpressZone(expressZoneInputDTO);

        return 1;
    }

其中这段可以说下,毕竟是反复改了几次::

//            //根据路径获取这个操作excel的实例
//            XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
//            //根据页面index 获取sheet页
//            XSSFSheet sheet = wb.getSheetAt(0);
            // 兼容2007以前或以后的excel版本
            Workbook workbook = WorkbookFactory.create(file.getInputStream());
            Sheet hssfSheet = workbook.getSheetAt(0);  //示意访问sheet

首先一个如果使用: XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());,这种格式只支持2007版本后的excel

而 XSSFWorkbook 又只支持2007版之前了,但是业务肯定不答应了,人家的电脑老旧点,你就不给用了,我不打死你哦。

所以才有了 Workbook workbook = WorkbookFactory.create(file.getInputStream()); 所以需要注意的。

另一个就是有个包容易引起报错:

<!--       去掉以下jar,不然会报冲突 org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException-->
<!--        <dependency>-->
<!--            <groupId>org.apache.poi</groupId>-->
<!--            <artifactId>poi-ooxml-schemas</artifactId>-->
<!--            <version>3.14</version>-->
<!--        </dependency>-->

报错已经很明确了,就是jar冲突,我删除了也没发现有何问题,暂时用不到。哈哈哈

领导来了,得撤了,编码不易,留个鼓励的赞再走吧。

如有问题一起探讨,欢迎微信:441338280.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值