The supplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents. You need to call a different part of POI to process this data (eg HSSF instead of XSSF)

在做Excel导入功能时,发现导入的文件报错,代码中判断了文件类型:

// IO流读取文件
InputStream input = file.getInputStream();
//解析xls
HSSFWorkbook wb = new HSSFWorkbook(input);
//解析xlsx
XSSFWorkbook wbx = new XSSFWorkbook(input);

但是依旧报错:

org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents. You need to call a different part of POI to process this data (eg HSSF instead of XSSF)

网上查询也都是版本问题。

后来反复测试,发现xls导入没问题,但是将文件后缀直接改成xlsx导入就会报错。

将文件另存为xlsx后,再次导入,就不在报错了。

总结:

修改文件类型时最好使用另存为,避免出现版本问题。

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐