From 352c5934e3f611aab8f8121635b81fe41a342834 Mon Sep 17 00:00:00 2001 From: lxyer <237809796@qq.com> Date: Fri, 21 Sep 2018 01:19:20 +0800 Subject: [PATCH] add readExcel for "NO fields" --- pom.xml | 5 ++ src/com/lxyer/excel/poi/ExcelKit.java | 65 +++++++++++++++++++++++++- test/com/lxyer/excel/ExcelKitTest.java | 47 +++++++++++++++++++ 3 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 test/com/lxyer/excel/ExcelKitTest.java diff --git a/pom.xml b/pom.xml index 9d89a48..07af22b 100644 --- a/pom.xml +++ b/pom.xml @@ -10,6 +10,11 @@ src test + + + res + + org.apache.maven.plugins diff --git a/src/com/lxyer/excel/poi/ExcelKit.java b/src/com/lxyer/excel/poi/ExcelKit.java index c05d419..62da15b 100644 --- a/src/com/lxyer/excel/poi/ExcelKit.java +++ b/src/com/lxyer/excel/poi/ExcelKit.java @@ -232,15 +232,31 @@ public class ExcelKit { return readExcel(file, fields, -1, null); } + //read excel sheet[0] no fields + public static List readExcel(File file) throws IOException { + return readExcel(file, null, -1, null); + } + + //read excel by sheetName public static List readExcel(File file, String[] fields, String sheetName) throws IOException { return readExcel(file, fields, -1, sheetName); } + //read excel by sheetName no fields + public static List readExcel(File file, String sheetName) throws IOException { + return readExcel(file, null, -1, sheetName); + } + //read excel all sheet public static Map> readExcelAll(File file, String[] fields) throws IOException { return readExcelAll(file, fields, -1); } + //read all excel no fields + public static Map> readExcelAll(File file) throws IOException { + return readExcelAll(file, null, -1); + } + //read excel sheet[0] private static List 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 readExcel(Sheet sheet, String[] fields, int lastRowNum) throws OfficeXmlFileException { + if (fields == null || fields.length == 0){ + return readExcel(sheet, lastRowNum); + } + List 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 readExcel(Sheet sheet, int lastRowNum) throws OfficeXmlFileException { + List 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++) { diff --git a/test/com/lxyer/excel/ExcelKitTest.java b/test/com/lxyer/excel/ExcelKitTest.java new file mode 100644 index 0000000..c9ad151 --- /dev/null +++ b/test/com/lxyer/excel/ExcelKitTest.java @@ -0,0 +1,47 @@ +package com.lxyer.excel; + +import com.lxyer.excel.poi.ExcelKit; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; + +/** + * @author: liangxianyou at 2018/9/15 7:52. + */ +public class ExcelKitTest { + + //read excel by fields + @Test + public void readTest() throws IOException { + /*String sql1 = "select a, b, c from Sheet1"; + String sql2 = "select a, b, c from Sheet1 where a=1 order by b"; + String sql3 = "select a, b, c from Sheet1 s1, Sheet2 s2 order by"; + String sql4 = "select a, b, c from Sheet1 s1, Sheet2 s2 where order by"; + String select = sql1.substring(0,sql1.indexOf("from")).replace("select", "").replace(" ", ""); + + System.out.println(sql1.substring(0,sql1.indexOf("from")).replace("select", "").replace(" ", "")); + */ + + String[] fields ={"a", "b", "c"}; + File file = new File("res/test.xlsx"); + List list = ExcelKit.readExcel(file, fields); + System.out.println(list.size()); + + } + + //read excel no fields + @Test + public void readTest2() throws IOException { + List list = ExcelKit.readExcel(new File("res/test.xlsx")); + + list.forEach(x->{ + x.forEach((k,v)->{ + System.out.println(String.format("%s:%s", k, v)); + }); + }); + } + +}