Apache POI读取excel文件异常:OfficeXmlFileException

代码截取如下:

// 根据excel文件路径创建文件流
FileInputStream fis = new FileInputStream("E://temp//HHOIG.xlsx"); 
// 利用poi读取excel文件流
POIFSFileSystem fs = new POIFSFileSystem(fis); 
 // 读取excel工作簿
HSSFWorkbook wb = new HSSFWorkbook(fs);
 

在运行时报错:

Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: 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)

原因:HSSFWorkbook只能处理扩展名为.xls的文件,也就是excel2007以前的文件。

解决办法:可以将.xlsx文件另存为.xls文件再进行处理(不是直接改扩展名)。

猜你喜欢

转载自freerunning.iteye.com/blog/1705496