diff --git a/src/com/engine/salary/util/excel/ExcelParseHelper.java b/src/com/engine/salary/util/excel/ExcelParseHelper.java index 0e9e0364b..c19bb7b8a 100644 --- a/src/com/engine/salary/util/excel/ExcelParseHelper.java +++ b/src/com/engine/salary/util/excel/ExcelParseHelper.java @@ -90,7 +90,7 @@ public class ExcelParseHelper { for (; rowIndex < rowCount; rowIndex++) { List cellResult = new ArrayList(); for (int j = 0; j < cellCount; j++) { - cellResult.add(ExcelSupport.getCellValue(sheet,null, rowIndex, j)); + cellResult.add(ExcelSupport.getCellValue(sheet, null, rowIndex, j)); } result.add(cellResult); } @@ -120,7 +120,7 @@ public class ExcelParseHelper { for (; rowIndex < rowCount; rowIndex++) { List cellResult = new ArrayList(); for (int j = 0; j < cellCount; j++) { - cellResult.add(ExcelSupport.getCellValue(sheet,evaluator, rowIndex, j)); + cellResult.add(ExcelSupport.getCellValue(sheet, evaluator, rowIndex, j)); } result.add(cellResult); } @@ -137,7 +137,10 @@ public class ExcelParseHelper { * @return */ public static List> parse2Map(InputStream file, int sheetIndex, int rowIndex) { - Sheet sheet = ExcelSupport.parseFile(file, sheetIndex, EXCEL_TYPE_XLSX); + Workbook workbook = ExcelSupport.parseFile(file, EXCEL_TYPE_XLSX); + // 创建一个公式求值器对象 + FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); + Sheet sheet = workbook.getSheetAt(sheetIndex); int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数 int cellCount = sheet.getRow(PARSE_EXCEL_ROW_VALID_CELL_INDEX).getPhysicalNumberOfCells(); // 总列数 @@ -148,7 +151,7 @@ public class ExcelParseHelper { Map cellResult = new HashMap<>(); for (int j = 0; j < cellCount; j++) { String key = sheetHeader.get(j); - cellResult.put(key, ExcelSupport.getCellValue(sheet,null, rowIndex, j)); + cellResult.put(key, ExcelSupport.getCellValue(sheet, evaluator, rowIndex, j)); } result.add(cellResult); } @@ -173,7 +176,28 @@ public class ExcelParseHelper { Map cellResult = new HashMap<>(); for (int j = 0; j < cellCount; j++) { String key = sheetHeader.get(j); - cellResult.put(key, ExcelSupport.getCellValue(sheet, null,rowIndex, j)); + cellResult.put(key, ExcelSupport.getCellValue(sheet, null, rowIndex, j)); + } + result.add(cellResult); + } + return result; + } + + public static List> parse2Map(Workbook workbook, int sheetIndex, int rowIndex) { + // 创建一个公式求值器对象 + FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); + Sheet sheet = workbook.getSheetAt(sheetIndex); + int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数 + int cellCount = sheet.getRow(PARSE_EXCEL_ROW_VALID_CELL_INDEX).getPhysicalNumberOfCells(); // 总列数 + + List sheetHeader = ExcelSupport.getSheetHeader(sheet, PARSE_EXCEL_ROW_VALID_CELL_INDEX); + + List> result = new ArrayList<>(); + for (; rowIndex < rowCount; rowIndex++) { + Map cellResult = new HashMap<>(); + for (int j = 0; j < cellCount; j++) { + String key = sheetHeader.get(j); + cellResult.put(key, ExcelSupport.getCellValue(sheet, evaluator, rowIndex, j)); } result.add(cellResult); } @@ -183,7 +207,7 @@ public class ExcelParseHelper { /** * 将sheet数据转为map * - * @param rowIndex 从哪行开始解析 + * @param rowIndex 从哪行开始解析 * @param headerRowIndex 抽取列数的参考行 * @return */ @@ -198,7 +222,7 @@ public class ExcelParseHelper { Map cellResult = new HashMap<>(); for (int j = 0; j < cellCount; j++) { String key = sheetHeader.get(j); - cellResult.put(key, ExcelSupport.getCellValue(sheet, null,rowIndex, j)); + cellResult.put(key, ExcelSupport.getCellValue(sheet, null, rowIndex, j)); } result.add(cellResult); } @@ -219,7 +243,7 @@ public class ExcelParseHelper { for (; rowIndex < rowCount; rowIndex++) { List cellResult = new ArrayList(); for (int j = 0; j < cellCount; j++) { - cellResult.add(ExcelSupport.getCellValue(sheet,null, rowIndex, j)); + cellResult.add(ExcelSupport.getCellValue(sheet, null, rowIndex, j)); } result.add(cellResult); } @@ -228,7 +252,8 @@ public class ExcelParseHelper { /** * 将sheet数据转为List - * @param rowIndex 从哪行开始解析 + * + * @param rowIndex 从哪行开始解析 * @param headerRowIndex 抽取列数的参考行 * @return */ @@ -240,7 +265,7 @@ public class ExcelParseHelper { for (; rowIndex < rowCount; rowIndex++) { List cellResult = new ArrayList(); for (int j = 0; j < cellCount; j++) { - cellResult.add(ExcelSupport.getCellValue(sheet,null, rowIndex, j)); + cellResult.add(ExcelSupport.getCellValue(sheet, null, rowIndex, j)); } result.add(cellResult); } @@ -249,11 +274,12 @@ public class ExcelParseHelper { /** * 将sheet数据转为List - * @param rowIndex 从哪行开始解析 + * + * @param rowIndex 从哪行开始解析 * @param headerRowIndex 抽取列数的参考行 * @return */ - public static List> parse2List(Sheet sheet, FormulaEvaluator evaluator ,int rowIndex, int headerRowIndex) { + public static List> parse2List(Sheet sheet, FormulaEvaluator evaluator, int rowIndex, int headerRowIndex) { int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数 int cellCount = sheet.getRow(headerRowIndex).getPhysicalNumberOfCells(); // 总列数 @@ -261,7 +287,7 @@ public class ExcelParseHelper { for (; rowIndex < rowCount; rowIndex++) { List cellResult = new ArrayList(); for (int j = 0; j < cellCount; j++) { - cellResult.add(ExcelSupport.getCellValue(sheet,evaluator, rowIndex, j)); + cellResult.add(ExcelSupport.getCellValue(sheet, evaluator, rowIndex, j)); } result.add(cellResult); } @@ -343,8 +369,8 @@ public class ExcelParseHelper { // 创建一个公式求值器对象 FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); Sheet sheet = workbook.getSheetAt(i); - return ExcelPreviewDTO.builder() - .headers(ExcelSupport.getSheetHeader(sheet, 0)) - .list(ExcelParseHelper.parse2List(sheet, evaluator ,1, 0)).build(); + return ExcelPreviewDTO.builder() + .headers(ExcelSupport.getSheetHeader(sheet, 0)) + .list(ExcelParseHelper.parse2List(sheet, evaluator, 1, 0)).build(); } }