POI Excel兼容.xlsx和.xls格式

Apache POI兼容excel .xlsx和.xls格式,使用WorkbookFactory替换HSSF(HSSFWorkbook)和XSSF(XSSFWorkbook)。

resolved

import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Workbook;

使用WorkbookFactory.create(InputStream in) API读取excel兼容两种格式:

// Handles both XSSF and HSSF automatically
Workbook workbook = WorkbookFactory.create(new FileInputStream(file)); 

或使用Workbook接口对HSSFWorkbook和XSSFWorkbook对象引用进行泛化。

.xls格式:

Workbook wb = new HSSFWorkbook();

.xlsx格式:

Workbook wb = new XSSFWorkbook();