【开发日志】XLS文件无法识别--Convert excel format exception.You can try specifying the ‘excelType‘ yourself 解决方法

业务中用到EasyExcle,两个接口(解析excel,写入数据)在apipost中单独调用一切正常,到网页组合业务后出现以上报错信息。

通过一段时间的搜索,尝试解决问题

分析问题可能发生的原因如下:

  1. 参考https://blog.csdn.net/Smy_0114/article/details/126152151 可能是流部分出现了问题

    我这里的业务首先需要读取exel进行解析表头,返回表头给前端显示,这是第一次调用,这个方法也没问题

    第二次调用EasyExcel是要解析数据 把数据做一个写入的操作,也就是说这里上传了两次文件,出现了上述流的问题

  2. 参考https://blog.csdn.net/weixin_39970883/article/details/129316000 可能出现了传输和接收文件格式不对等的情况

    通过debug,可以看到过来的流依然是MutipartFile这个排除

  3. 参考 https://github.com/alibaba/easyexcel/issues/3296 可能出现因为没有指定上传文件的类型而报错 在添加.excelType(xxx)后会有新的错误 提示xlsx,xls文件格式过老或无法解析 报错如下

    org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0x207463656A626F5B, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
    

    也不是解决办法的途径

解决办法:

  1. 在文件解析时,先把文件写入服务器的目录下,保存起来,解析后回传给前端一个文件名称

    try{
    	File dictionary = new File(mutipartFile.getOrignalFilename());
    	String uploadPath = dictionary.getCanonicalPath()+"/xxxx"
    	if(!dictionary.exists()){
    		dictionary.mkdir();
    	}
    	byte[] bytes = mutipartFile.getBytes();
    	Path path = Paths.get(uploadPath,mutipartFile.getOrignalFilename());
    	Files.write(path,bytes);
    }cath(IOException e){
    	return e;
    }
    
  2. 要做数据的写入时,去服务器目录下拿出文件,转为流/直接处理

    Path path = null;
    File dictionary = new File("");
    String uploadPath = dictionary.getCanonicalPath()+"/xxxx"
    path = Paths.get(uploadPath,filename);
    file = path.toFile()
    EasyExcleListener readListener = new EasyExcleListener();
    EasyEcel.read(file,readListener),sheet().doRead();
    

经测试 这样就不报问题了

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值