diff --git a/build.gradle b/build.gradle index 9b648ed..16d70b0 100644 --- a/build.gradle +++ b/build.gradle @@ -59,7 +59,7 @@ uploadArchives { authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) } pom.project { - version '1.1.32' + version '1.1.34' artifactId ARTIFACT_Id groupId GROUP_ID packaging TYPE diff --git a/src/main/java/com/taover/util/UtilExcel.java b/src/main/java/com/taover/util/UtilExcel.java index 896234a..1b634bc 100644 --- a/src/main/java/com/taover/util/UtilExcel.java +++ b/src/main/java/com/taover/util/UtilExcel.java @@ -237,26 +237,24 @@ public class UtilExcel { */ public static List> readExcel(String filepath) throws Exception{ File file = new File(filepath); - List> result = new ArrayList>(); if(!file.exists()){ - return result; + throw new Exception("Excel["+filepath+"]文件不存在"); } Workbook wb = UtilExcel.getWorkbook(filepath, true); - return readExcelBySheetIndex(filepath, wb.getActiveSheetIndex()); + return readExcelBySheetIndex(wb, wb.getActiveSheetIndex()); } /** * 读取excel表--当前激活的工作表 * @param path */ - public static List> readExcel(String filepath, int rowLimit) throws Exception{ + public static List> readExcel(String filepath, boolean hasLimit) throws Exception{ File file = new File(filepath); - List> result = new ArrayList>(); if(!file.exists()){ - return result; + throw new Exception("Excel["+filepath+"]文件不存在"); } Workbook wb = UtilExcel.getWorkbook(filepath, true); - return readExcelBySheetIndex(filepath, wb.getActiveSheetIndex()); + return readExcelBySheetIndex(wb, wb.getActiveSheetIndex(), hasLimit); } /** @@ -267,9 +265,8 @@ public class UtilExcel { */ public static List> readExcelExcludeHideLine(String filepath) throws Exception{ File file = new File(filepath); - List> result = new ArrayList>(); if(!file.exists()){ - return result; + throw new Exception("Excel["+filepath+"]文件不存在"); } Workbook wb = UtilExcel.getWorkbook(filepath, true); return readExcelBySheetIndexExcludeHideLine(filepath, wb.getActiveSheetIndex()); @@ -283,12 +280,12 @@ public class UtilExcel { File file = new File(filepath); List> result = new ArrayList>(); if(!file.exists()){ - return result; + throw new Exception("Excel["+filepath+"]文件不存在"); } Workbook wb = UtilExcel.getWorkbook(filepath, true); int sheetNumber = wb.getNumberOfSheets(); - for(int sheetIndex=0; sheetIndex> readExcelBySheetIndex(String filepath, int sheetIndex) throws Exception{ File file = new File(filepath); - List> result = new ArrayList>(); if(!file.exists()){ - return result; + throw new Exception("Excel["+filepath+"]文件不存在"); } - Workbook wb = UtilExcel.getWorkbook(filepath, true); - //创建Excel工作簿对象 - DecimalFormat df = new DecimalFormat("0"); + Workbook wb = UtilExcel.getWorkbook(filepath, true); Sheet sheet = wb.getSheetAt(sheetIndex); - int start = sheet.getFirstRowNum(); int end = sheet.getLastRowNum(); if(end > UtilExcel.maxExcelRowNum){ throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大"); } - for(int i=start; i dataRow = new ArrayList(); - int lastCellNum = row.getLastCellNum(); - if(lastCellNum > UtilExcel.maxExcelColumnNum){ - lastCellNum = UtilExcel.maxExcelColumnNum; - } - for(int j=0; j> readExcelBySheetIndex(Workbook wb, int sheetIndex) throws Exception{ + return readExcelBySheetIndex(wb, sheetIndex, true); + } + + private static List> readExcelBySheetIndex(Workbook wb, int sheetIndex, boolean hasRowLimit) throws Exception{ + Sheet sheet = wb.getSheetAt(sheetIndex); + int end = sheet.getLastRowNum(); + if(hasRowLimit){ + if(end > UtilExcel.maxExcelRowNum) { + throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大"); } - result.add(dataRow); + return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, false); + }else { + return readExcelBySheet(sheet, Integer.MAX_VALUE, false); } - return result; } /** * 读取excel表--当前激活的工作表 * @param path */ - public static List> readExcelBySheet(Sheet sheet, int rowLimit) throws Exception{ + private static List> readExcelBySheet(Sheet sheet, int rowLimit, boolean excludeRowZeroHeight) throws Exception{ List> result = new ArrayList>(); int start = sheet.getFirstRowNum(); int end = sheet.getLastRowNum(); @@ -354,6 +344,10 @@ public class UtilExcel { if(row == null){ continue; } + //去除隐藏行 + if(excludeRowZeroHeight && row.getZeroHeight()){ + continue; + } List dataRow = new ArrayList(); int lastCellNum = row.getLastCellNum(); if(lastCellNum > UtilExcel.maxExcelColumnNum){ @@ -381,13 +375,11 @@ public class UtilExcel { if(currCellType.compareTo(CellType.STRING) == 0){ return UtilString.trimCodePage(cell.getRichStringCellValue().getString()); }else if(currCellType.compareTo(CellType.NUMERIC) == 0){ - - //if (DateUtil.isCellDateFormatted(cell)) { if (cell.getCellStyle().getDataFormat()==28||cell.getCellStyle().getDataFormat()==31 || cell.getCellStyle().getDataFormat() == 58) { // 如果是date类型则 ,获取该cell的date值 return new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue())); - } else { // 纯数字 - // return String.valueOf(cell.getNumericCellValue()); + } else { + // 纯数字 return df.format(cell.getNumericCellValue()); } }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){ @@ -415,114 +407,17 @@ public class UtilExcel { public static List> readExcelBySheetIndexExcludeHideLine(String filepath, int sheetIndex) throws Exception{ File file = new File(filepath); - List> result = new ArrayList>(); if(!file.exists()){ - return result; + throw new Exception("Excel文件["+filepath+"]不存在"); } - Workbook wb = UtilExcel.getWorkbook(filepath, true); - //创建Excel工作簿对象 - DecimalFormat df = new DecimalFormat("0"); + Workbook wb = UtilExcel.getWorkbook(filepath, true); Sheet sheet = wb.getSheetAt(sheetIndex); - int start = sheet.getFirstRowNum(); int end = sheet.getLastRowNum(); if(end > UtilExcel.maxExcelRowNum){ throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大"); } - for(int i=start; i dataRow = new ArrayList(); - int lastCellNum = row.getLastCellNum(); - if(lastCellNum > UtilExcel.maxExcelColumnNum){ - lastCellNum = UtilExcel.maxExcelColumnNum; - } - for(int j=0; j> readExcelBySheetExcludeHideLine(Sheet sheet, int rowLimit) throws Exception{ - List> result = new ArrayList>(); - DecimalFormat df = new DecimalFormat("0"); - int start = sheet.getFirstRowNum(); - int end = sheet.getLastRowNum(); - if(end > rowLimit){ - end = rowLimit; - } - for(int i=start; i dataRow = new ArrayList(); - int lastCellNum = row.getLastCellNum(); - if(lastCellNum > UtilExcel.maxExcelColumnNum){ - lastCellNum = UtilExcel.maxExcelColumnNum; - } - for(int j=0; j