diff --git a/src/excel/poi/ExcelKit.java b/src/excel/poi/ExcelKit.java index feed22d..430c3ac 100644 --- a/src/excel/poi/ExcelKit.java +++ b/src/excel/poi/ExcelKit.java @@ -1,17 +1,11 @@ package excel.poi; -import org.apache.poi.hssf.usermodel.HSSFCell; -import org.apache.poi.hssf.usermodel.HSSFRow; -import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.OfficeXmlFileException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.xssf.usermodel.XSSFCell; -import org.apache.poi.xssf.usermodel.XSSFRow; -import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import javax.servlet.http.HttpServletRequest; @@ -219,61 +213,91 @@ public class ExcelKit { } } - //读取 excel - //读取excel表头 - public static Map readExcelHead(File file, String[] fields) throws IOException { - List list = null; + //======================= 读取excel =============================== - try { - list = readExcel(new FileInputStream(file), fields, 0);//2007 - - } catch (OfficeXmlFileException e) { - list = readExcelPlus(new FileInputStream(file), fields, 0);//2007 + - } - return list.size() > 0 ? list.get(0) : new HashMap(); - } - - //读取excel表头 - public static Map readExcelHead(String fPath, String[] fields) throws IOException { - return readExcelHead(new File(fPath), fields); + /** + * read excel head + * @param file + * @param fields + * @param lastRowNum hope get row count + * @return + * @throws IOException + */ + public static List readExcelHead(File file, String[] fields, int lastRowNum) throws IOException { + return readExcel(file, fields, lastRowNum, null); } + //read excel sheet[0] public static List readExcel(File file, String[] fields) throws IOException { - List list = null; - - try { - list = readExcel(new FileInputStream(file), fields, -1);//2007 - - } catch (OfficeXmlFileException e) { - list = readExcelPlus(new FileInputStream(file), fields, -1);//2007 + - } - - //去除表头 - if (list.size() > 0){ - list.remove(0); - } - return list; + return readExcel(file, fields, -1, null); } - public static List readExcel(String fPath, String[] fields) throws IOException { - return readExcel(new File(fPath), fields); + public static List readExcel(File file, String[] fields, String sheetName) throws IOException { + return readExcel(file, fields, -1, sheetName); + } + + //read excel all sheet + public static Map> readExcelAll(File file, String[] fields) throws IOException { + return readExcelAll(file, fields, -1); + } + + //read excel sheet[0] + private static List readExcel(File file, String[] fields, int lastRowNum, String sheetName) throws IOException { + Workbook wk; + try { + wk = new HSSFWorkbook(new FileInputStream(file));//if excel version 2007+ will throws OfficeXmlFileException + }catch (OfficeXmlFileException e){ + wk = new XSSFWorkbook(new FileInputStream(file)); + } + Sheet sheet = sheetName == null ? wk.getSheetAt(0) : wk.getSheet(sheetName); + return readExcel(sheet, fields, lastRowNum); } /** - * excel读取2007- + * read excel all sheet, + * can read excel 2007+ and 2007- + * @param file + * @param fields + * @param lastRowNum + * @return + * @throws IOException + */ + private static Map> readExcelAll(File file, String[] fields, int lastRowNum) throws IOException { + Workbook wk; + try { + wk = new HSSFWorkbook(new FileInputStream(file));//if excel version 2007+ will throws OfficeXmlFileException + }catch (OfficeXmlFileException e){ + wk = new XSSFWorkbook(new FileInputStream(file)); + } + + Map> data = new LinkedHashMap<>(); + //reading all sheet + for (int i = 0; i < wk.getNumberOfSheets(); i++) { + Sheet sheet = wk.getSheetAt(i); + List maps = readExcel(sheet, fields, lastRowNum); + + //move head + if (lastRowNum < 0 && maps.size() > 0){ + maps.remove(0); + } + data.put(sheet.getSheetName(), maps); + } + return data; + } + + /** + * read excel * @author Lxyer 2016/8/1 10:32. */ - private static List readExcel(InputStream is, String[] fields, int lastRowNum) throws IOException,OfficeXmlFileException { + private static List readExcel(Sheet sheet, String[] fields, int lastRowNum) throws OfficeXmlFileException { List list = new ArrayList<>(); - - HSSFWorkbook wk = new HSSFWorkbook(is); - HSSFSheet sheet = wk.getSheetAt(0); - if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){ lastRowNum = sheet.getLastRowNum(); } int t = 0; r:for (int i=0; i<=lastRowNum; i++){ - HSSFRow row = sheet.getRow(i); + Row row = sheet.getRow(i); if (row == null) continue ; short cellNum = row.getLastCellNum(); //空跳过/连续三行为空结束 @@ -284,7 +308,7 @@ public class ExcelKit { Map map = new HashMap(); for (int j=0; j readExcelPlus(InputStream is, String[] fields, int lastRowNum) throws IOException { - List list = new ArrayList<>(); - - XSSFWorkbook wk = new XSSFWorkbook(is); - XSSFSheet sheet = wk.getSheetAt(0); - - if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){ - lastRowNum = sheet.getLastRowNum(); - } - - int t=0; - r:for (int i=0; i<=lastRowNum; i++){ - XSSFRow row = sheet.getRow(i); - if (row == null) continue ; - short cellNum = row.getLastCellNum(); - - if (isEmptyRow(row, fields.length)) { - if (t++ > 3) break; - continue ; - } - - Map map = new HashMap(); - //读取数据 - for (int j=0; j