public static List ImportExcel(File file) {
List list = new ArrayList();
if (file.getName().indexOf(".xls") > 0) {
try {
Workbook wb = Workbook.getWorkbook(file);
Sheet sheet = wb.getSheet(0);
int rowCount = sheet.getRows();
int cols = sheet.getColumns();
for (int i = 3; i < rowCount; i++) {
String[] temp = new String[cols];
for (int j = 0; j < cols; j++) {
Cell cell = sheet.getCell(j, i);
temp[j] = cell.getContents();
}
list.add(temp);
}
} catch (Exception ex) {
ex.printStackTrace();
list = null;
}
}
return list;
}
传入一个文件地址,返回一个List,后在导入数据库中。
注意:需要导入一个JXL.jar文件。