poi同时解析03 和07 excel

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

 

Workbook wb = null;
FileInputStream stream = null;

try {

stream = new FileInputStream(savedFileName);
wb = WorkbookFactory.create(stream);
/*if (savedFileName.endsWith(".xls")) {

wb = new HSSFWorkbook(stream);// 解析03版本
}else if (savedFileName.endsWith(".xlsx")) {
wb = new XSSFWorkbook(stream); // 解析07版本以上的

}*/


} catch (Exception e) {
throw new CacpsException("载入文件失败,请确认文件格式是否正确!" + e.getMessage());
} finally {
try {
stream.close();
} catch (Exception ee) {
throw new CacpsException("关闭文件失败!" + ee.getMessage());
}
}

你可能感兴趣的:(poi同时解析03 和07 excel)