public classExcelUtil {public static List>readXlsx(String path){
List> result=new ArrayList>();try{
InputStream input=newFileInputStream(path);
XSSFWorkbook workbook=newXSSFWorkbook(input);for(XSSFSheet xssfSheet:workbook){if(xssfSheet==null){continue;
}for(int rowNum=1;rowNum<=xssfSheet.getLastRowNum();rowNum++){
XSSFRow row=xssfSheet.getRow(rowNum);int minCellNum=row.getFirstCellNum();int maxCellNum=row.getLastCellNum();
ListrowList=new ArrayList();for(int i=minCellNum;i
XSSFCell cell=row.getCell(i);if(cell==null){continue;
}
rowList.add(cell.toString());
}
result.add(rowList);
}
}
}catch(FileNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnresult;
}public static List>readXls(String path){
List> result=new ArrayList>();try{
InputStream input=newFileInputStream(path);
HSSFWorkbook workbook=newHSSFWorkbook(input);for(int numSheet=0;numSheet
HSSFSheet sheet=workbook.getSheetAt(numSheet);if(sheet==null){continue;
}for(int rowNum=1;rowNum<=sheet.getLastRowNum();rowNum++){
HSSFRow row=sheet.getRow(rowNum);int minCellNum=row.getFirstCellNum();int maxCellNum=row.getLastCellNum();
List rowList=new ArrayList();for(int i=minCellNum;i
HSSFCell cell=row.getCell(i);if(cell==null){continue;
}
rowList.add(getStringVal(cell));
}
result.add(rowList);
}
}
}catch(FileNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnresult;
}private staticString getStringVal(HSSFCell cell) {switch(cell.getCellType()) {caseCell.CELL_TYPE_BOOLEAN:return cell.getBooleanCellValue() ? "TRUE" : "FALSE";caseCell.CELL_TYPE_FORMULA:returncell.getCellFormula();caseCell.CELL_TYPE_NUMERIC:
cell.setCellType(Cell.CELL_TYPE_STRING);returncell.getStringCellValue();caseCell.CELL_TYPE_STRING:returncell.getStringCellValue();default:return null;
}
}
}