流读取文件时可能报以下错误:
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文件内新增或补足以下代码块文章来源:https://www.toymoban.com/news/detail-849579.html
<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类,样例如下:文章来源地址https://www.toymoban.com/news/detail-849579.html
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) {
}
到了这里,关于java.io.IOException: Your InputStream was neither an OLE2 stream, nor an OOXML stream的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!