栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Convert excel format exception.You can try specifying the ‘excelType‘ yourself

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Convert excel format exception.You can try specifying the ‘excelType‘ yourself

    @ApiOperation(value = "楼栋基本信息", notes = "需要登录。请求头携带Authorization字段")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "excelFile", value = "excel文件", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = -1, message = "错误响应"),
            @ApiResponse(code = 2000, message = "成功响应")
    })
    @PostMapping("/odsBulidingInfo")
    public baseResponse odsBulidingInfo(@RequestParam("excelFile") @ApiIgnore MultipartFile multipartfile) {
        if(multipartfile==null){
            return baseResponse.error("没有上传文件");
        }
        if(multipartfile.isEmpty()){
            return baseResponse.error("上传文件是空");
        }


        try{
            InputStream inputStream = multipartfile.getInputStream();
            String fileType = CheckFileFormatUtil.getFileType(inputStream);
            if(!StringUtils.equals(Constant.FileType.XLSX, fileType)){
                return baseResponse.error("只能上传xlsx类型文件");
            }


            ExcelTool.read(inputStream, OdsBulidingInfo.class, new InsertDB() {
                @Override
                public void save(List dataList){
                    for(OdsBulidingInfo item : dataList) {
                        item.setBuliding_info_id(IdTool.getIdForTable());
                        int ii = importExcelMapper.odsBulidingInfo(item);
                    }
                }
            });
        }catch(Exception e){
            throw new MyException(baseResponse.error(e.getMessage()));
        }


        return baseResponse.success();
    }

上传Excel解析时报错:Convert excel format exception.You can try specifying the ‘excelType‘ yourself

原因是:因为在通过file获取的流类型,在EasyExcel.read读的时候,采用的同一个流,会导致excel类型出错。

解决方法:重新创建一个excel流,与获取文件流类型的流区别使用 

InputStream inputStream = multipartfile.getInputStream();
String fileType = CheckFileFormatUtil.getFileType(multipartfile.getInputStream());

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/270850.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号