add readExcel for "NO fields"

This commit is contained in:
2018-09-21 01:19:20 +08:00
parent a10f7e3680
commit 352c5934e3
3 changed files with 115 additions and 2 deletions

View File

@@ -232,15 +232,31 @@ public class ExcelKit {
return readExcel(file, fields, -1, null);
}
//read excel sheet[0] no fields
public static List<Map> readExcel(File file) throws IOException {
return readExcel(file, null, -1, null);
}
//read excel by sheetName
public static List<Map> readExcel(File file, String[] fields, String sheetName) throws IOException {
return readExcel(file, fields, -1, sheetName);
}
//read excel by sheetName no fields
public static List<Map> readExcel(File file, String sheetName) throws IOException {
return readExcel(file, null, -1, sheetName);
}
//read excel all sheet
public static Map<String, List<Map>> readExcelAll(File file, String[] fields) throws IOException {
return readExcelAll(file, fields, -1);
}
//read all excel no fields
public static Map<String, List<Map>> readExcelAll(File file) throws IOException {
return readExcelAll(file, null, -1);
}
//read excel sheet[0]
private static List<Map> readExcel(File file, String[] fields, int lastRowNum, String sheetName) throws IOException {
Workbook wk;
@@ -291,6 +307,10 @@ public class ExcelKit {
* @author Lxyer 2016/8/1 10:32.
*/
private static List<Map> readExcel(Sheet sheet, String[] fields, int lastRowNum) throws OfficeXmlFileException {
if (fields == null || fields.length == 0){
return readExcel(sheet, lastRowNum);
}
List<Map> list = new ArrayList<>();
if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){
lastRowNum = sheet.getLastRowNum();
@@ -315,8 +335,7 @@ public class ExcelKit {
continue;
}
int cellType = cell.getCellType();
if (cellType == 0){
if (cell.getCellTypeEnum() == CellType.NUMERIC){
map.put(fields[j], (long)cell.getNumericCellValue()+"");
}else {
map.put(fields[j], cell.getStringCellValue());
@@ -327,6 +346,48 @@ public class ExcelKit {
return list;
}
/**
*
* @author Lxyer 2016/9/21 00:38.
*/
private static List<Map> readExcel(Sheet sheet, int lastRowNum) throws OfficeXmlFileException {
List<Map> list = new ArrayList<>();
if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){
lastRowNum = sheet.getLastRowNum();
}
int t = 0;
r:for (int i=0; i<=lastRowNum; i++){
Row row = sheet.getRow(i);
if (row == null) continue ;
short cellNum = row.getLastCellNum();
//空跳过/连续三行为空结束
if (isEmptyRow(row, 3)) {
if (t++ > 3) break;
continue ;
}
Map map = new HashMap();
for (int j=0; j < cellNum; j++){
String field = (char)((j/26 ==0? ' ' : 'a')+j/26) + "" +(char)('a'+j%26);
field = field.replace(" ","");
Cell cell = row.getCell(j);
if (cell == null){
map.put(field, "");
continue;
}
if (cell.getCellTypeEnum() == CellType.NUMERIC){
map.put(field, (long)cell.getNumericCellValue()+"");
}else {
map.put(field, cell.getStringCellValue());
}
}
list.add(map);
}
return list;
}
//空跳过/连续三行为空结束
private static boolean isEmptyRow(Row row, int len){
for (int i = 0; i< row.getLastCellNum() && i < len; i++) {