首页 > 基础资料 博客日记

java.io.IOException: Your InputStream was neither an OLE2 stream, nor an OOXML stream

2024-03-25 06:00:06基础资料围观376

Java资料网推荐java.io.IOException: Your InputStream was neither an OLE2 stream, nor an OOXML stream这篇文章给大家,欢迎收藏Java资料网享受知识的乐趣

流读取文件时可能报以下错误:

java.io.IOException: Your InputStream was neither an OLE2 stream, nor an OOXML stream
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:258)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:221)
at com.ruoyi.common.utils.poi.ExcelUtil.importExcel(ExcelUtil.java:186)
at com.ruoyi.common.utils.poi.ExcelUtil.importExcel(ExcelUtil.java:173)

此错误一般是文件上传时,用Workbook工厂或者实体类直接创建new HSSFWorkbook/XSSFWorkbook时出现。
解决方法很简单有两种:
1、在项目的pom.xml文件内新增或补足以下代码块

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
        <encoding>UTF-8</encoding>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
            <nonFilteredFileExtension>xls</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
    </configuration>
</plugin>

2、用更简单的cn.hutool.poi.excel.ExcelReader类,样例如下:

import cn.hutool.poi.excel.ExcelReader;
import org.springframework.web.multipart.MultipartFile;

	try (
	        InputStream inputStream = file.getInputStream()
	) {
	    ExcelReader reader = ExcelUtil.getReader(inputStream).setIgnoreEmptyRow(true);
	    List<Sheet> sheets = reader.getSheets();
	    Sheet sheet = sheets.get(0);//根据sheet的下标取值
	} catch (IOException e) {
	
	}

文章来源:https://blog.csdn.net/spec_king/article/details/132232237
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云