POI读取excel报错

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

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

经常会遇到

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文件

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

参考文献


bug生产者
20 声望0 粉丝