//根据03还是07版本的Excel创建不同wb对象
public static Workbook getWorkbook(InputStream inStr, String filePath) throws Exception {
Workbook wb = null;
String fileType = filePath.substring(filePath.lastIndexOf("."));
System.out.println(fileType);
if (".xls".equals(fileType)) {
wb = new HSSFWorkbook(inStr); // 2003-
} else if (".xlsx".equals(fileType)) {
wb = new XSSFWorkbook(inStr); // 2007+
} else {
throw new Exception("解析的文件格式有误!");
}
return wb;
}
// 读取多个sheet页面方法
private static Map<String, List<List<String>>> moreSheetImport(String filePath) throws Exception {
Map<String, List<List<String>>> tempMap = new HashMap<>();
File file = new File(filePath);
InputStream is = new FileInputStream(file);
Workbook wb = getWorkbook(is,filePath);
int sheetNum = wb.getNumberOfSheets();
for (int j = 0; j < sheetNum; j++) {
List<List<String>> lists = new ArrayList<List<String>>();
Sheet sheet = wb.getSheetAt(j);
int rowstart = sheet.getFirstRowNum();
int rowEnd = sheet.getLastRowNum();
for (int i = rowstart; i <= rowEnd; i++) {// 从第一行表头开始读
Row xssRow = sheet.getRow(i);
if (null == xssRow)
continue;
int cellStart = xssRow.getFirstCellNum();
if (cellStart > -1) {
int cellEnd = xssRow.getLastCellNum() - 1;//因为总大1,减去
List<String> listmini = new ArrayList<String>();
for (int k = cellStart; k <= cellEnd; k++) {
Cell cell = xssRow.getCell(k);
String value = getCellStringValue(cell);
listmini.add(value);
}
if (listmini.size() != 0) {
lists.add(listmini);
}
}
}
tempMap.put(String.valueOf(j), lists);
}
System.out.println(tempMap);
return tempMap;
}