0%

POI读取excel报错

POI读取excel报错

由于有时候上传的是xls文件有时候上传的是xlsx文件,使用POIFSFileSystem来读取文件

1
2
3
4
5
6
7
POIFSFileSystem poifsFileSystem;
try {
poifsFileSystem = new POIFSFileSystem(new FileInputStream(file));
} catch (IOException e) {
LOGGER.error("open xls file failure", e);
throw new BusinessException(1, "打不开xls文件");
}

经常会遇到

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

解决方法:

使用WorkbookFactory来读取excel文件

1
2
3
4
5
6
7
Workbook workbook;
try {
workbook = WorkbookFactory.create(new FileInputStream(file));
} catch (IOException e) {
LOGGER.error("open xls file failure", e);
throw new BusinessException(1, "打不开xls文件");
}

欢迎关注我的其它发布渠道