poi-4.1.0.jar
poi-ooxml-4.1.0.jar
poi-ooxml-schemas-4.1.0.jar
xmlbeans-3.1.0.jar
/**
* 处理导入的excel工具类
* @param file
* @return 返回List<List<String[]>>,Stringp[]内容按excel列排
* @throws Exception
*/
public static List<List<String[]>> importExcel(File file) throws Exception {
List<List<String[]>> excelDataList = new ArrayList<List<String[]>>();
Workbook wb = null ;
try {
FileInputStream fis = new FileInputStream(file);
if(file.getPath().endsWith("xls")){ // 2003
wb = new HSSFWorkbook(fis);
}else if(file.getPath().endsWith("xlsx")){ //2007
wb = new XSSFWorkbook(fis);
}
// 总共有多少张sheet表
int sheetnum = wb.getNumberOfSheets();
for (int i = 0; i < sheetnum; i++) {
Sheet sheet = wb.getSheetAt(i);
List<String[]> list = new ArrayList<String[]>();
// 数据
Row bodyrow = sheet.getRow(1);
if(bodyrow != null) {
// 总行数
int rowNum = sheet.getLastRowNum();
// 总列数
int colNum = bodyrow.getPhysicalNumberOfCells();
//循环行
for (int j = 1; j <= rowNum; j++) {
Row row = sheet.getRow(j);
if (row!=null) {
String[] strs = new String[colNum];
for(int y = 0; y < colNum; y++){
strs[y] = row.getCell(y).getStringCellValue();
}
list.add(strs);
}
}
}
excelDataList.add(list);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
wb.close();
file.delete();
}
return excelDataList;
}