diff --git a/src/com/engine/salary/service/SpecialAddDeductionService.java b/src/com/engine/salary/service/SpecialAddDeductionService.java index d2f893323..9879a6ab6 100644 --- a/src/com/engine/salary/service/SpecialAddDeductionService.java +++ b/src/com/engine/salary/service/SpecialAddDeductionService.java @@ -2,13 +2,16 @@ package com.engine.salary.service; import com.engine.salary.entity.datacollection.dto.SpecialAddDeductionListDTO; import com.engine.salary.entity.datacollection.dto.SpecialAddDeductionRecordDTO; -import com.engine.salary.entity.datacollection.param.*; +import com.engine.salary.entity.datacollection.param.SpecialAddDeductionImportParam; +import com.engine.salary.entity.datacollection.param.SpecialAddDeductionParam; +import com.engine.salary.entity.datacollection.param.SpecialAddDeductionQueryParam; +import com.engine.salary.entity.datacollection.param.SpecialAddDeductionRecordDeleteParam; import com.engine.salary.entity.datacollection.po.SpecialAddDeductionPO; +import com.engine.salary.util.excel.ExcelPreviewDTO; import com.engine.salary.util.page.PageInfo; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.time.YearMonth; -import java.util.Date; import java.util.List; import java.util.Map; @@ -61,7 +64,7 @@ public interface SpecialAddDeductionService { /** * 预览 */ - Map preview(SpecialAddDeductionImportParam importParam); + ExcelPreviewDTO preview(SpecialAddDeductionImportParam importParam); /** * 导入数据 diff --git a/src/com/engine/salary/service/impl/SpecialAddDeductionServiceImpl.java b/src/com/engine/salary/service/impl/SpecialAddDeductionServiceImpl.java index 7c8c14e64..a8e066705 100644 --- a/src/com/engine/salary/service/impl/SpecialAddDeductionServiceImpl.java +++ b/src/com/engine/salary/service/impl/SpecialAddDeductionServiceImpl.java @@ -38,7 +38,7 @@ import com.engine.salary.util.SalaryEntityUtil; import com.engine.salary.util.SalaryI18nUtil; import com.engine.salary.util.db.MapperProxyFactory; import com.engine.salary.util.excel.ExcelParseHelper; -import com.engine.salary.util.excel.ExcelSupport; +import com.engine.salary.util.excel.ExcelPreviewDTO; import com.engine.salary.util.excel.ExcelUtil; import com.engine.salary.util.page.PageInfo; import com.engine.salary.util.page.SalaryPageUtil; @@ -46,7 +46,6 @@ import com.google.common.collect.Maps; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; -import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.util.IOUtils; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import weaver.file.ImageFileManager; @@ -157,8 +156,7 @@ public class SpecialAddDeductionServiceImpl extends Service implements SpecialAd @Override - public Map preview(SpecialAddDeductionImportParam importParam) { - Map apidatas = new HashMap(); + public ExcelPreviewDTO preview(SpecialAddDeductionImportParam importParam) { //excel文件id String imageId = Util.null2String(importParam.getImageId()); @@ -167,10 +165,7 @@ public class SpecialAddDeductionServiceImpl extends Service implements SpecialAd InputStream fileInputStream = null; try { fileInputStream = ImageFileManager.getInputStreamById(Integer.parseInt(imageId)); - Sheet sheet = ExcelSupport.parseFile(fileInputStream, 0, EXCEL_TYPE_XLSX); - apidatas.put("headers", ExcelSupport.getSheetHeader(sheet, 0)); - apidatas.put("list", ExcelParseHelper.parse2List(sheet, 1, 0)); - return apidatas; + return ExcelParseHelper.preview(fileInputStream, 0, EXCEL_TYPE_XLSX); } finally { IOUtils.closeQuietly(fileInputStream); } @@ -249,7 +244,7 @@ public class SpecialAddDeductionServiceImpl extends Service implements SpecialAd //筛选导入人员信息可以在人力资源池中匹配到的人员信息 List emps = getSalaryEmployeeService(user) - .matchImportEmployee(confValue, employees, userName, deparmentName, mobile, workcode, idNo,null); + .matchImportEmployee(confValue, employees, userName, deparmentName, mobile, workcode, idNo, null); //含在职和离职,选在职数据 if (CollectionUtils.isNotEmpty(emps) && emps.size() > 1) { employeeSameIds = emps.stream() diff --git a/src/com/engine/salary/util/excel/ExcelParseHelper.java b/src/com/engine/salary/util/excel/ExcelParseHelper.java index c25d8b08d..0e9e0364b 100644 --- a/src/com/engine/salary/util/excel/ExcelParseHelper.java +++ b/src/com/engine/salary/util/excel/ExcelParseHelper.java @@ -3,7 +3,9 @@ package com.engine.salary.util.excel; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.exception.ContextedRuntimeException; +import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.springframework.web.multipart.MultipartFile; import java.io.InputStream; @@ -88,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, rowIndex, j)); + cellResult.add(ExcelSupport.getCellValue(sheet,null, rowIndex, j)); } result.add(cellResult); } @@ -104,17 +106,21 @@ public class ExcelParseHelper { * @return 二维数据集合 */ private static List> parse2Map(InputStream file, int sheetIndex, int rowIndex, int standardCellNum, String fileName) { - Sheet sheet = ExcelSupport.parseFile(file, sheetIndex, fileName); + Workbook workbook = ExcelSupport.parseFile(file, fileName); + Sheet sheet = workbook.getSheetAt(sheetIndex); int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数 int cellCount = sheet.getRow(PARSE_EXCEL_ROW_VALID_CELL_INDEX).getPhysicalNumberOfCells(); // 总列数 Validate.isTrue(standardCellNum == cellCount, "Error in excel template! Page %s sheet should have %s column data, existing in %s column , please check the template!", sheetIndex, standardCellNum, cellCount); + // 创建一个公式求值器对象 + FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); + List> result = new ArrayList>(); for (; rowIndex < rowCount; rowIndex++) { List cellResult = new ArrayList(); for (int j = 0; j < cellCount; j++) { - cellResult.add(ExcelSupport.getCellValue(sheet, rowIndex, j)); + cellResult.add(ExcelSupport.getCellValue(sheet,evaluator, rowIndex, j)); } result.add(cellResult); } @@ -142,7 +148,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, rowIndex, j)); + cellResult.put(key, ExcelSupport.getCellValue(sheet,null, rowIndex, j)); } result.add(cellResult); } @@ -167,7 +173,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, rowIndex, j)); + cellResult.put(key, ExcelSupport.getCellValue(sheet, null,rowIndex, j)); } result.add(cellResult); } @@ -192,7 +198,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, rowIndex, j)); + cellResult.put(key, ExcelSupport.getCellValue(sheet, null,rowIndex, j)); } result.add(cellResult); } @@ -213,7 +219,7 @@ public class ExcelParseHelper { for (; rowIndex < rowCount; rowIndex++) { List cellResult = new ArrayList(); for (int j = 0; j < cellCount; j++) { - cellResult.add(ExcelSupport.getCellValue(sheet, rowIndex, j)); + cellResult.add(ExcelSupport.getCellValue(sheet,null, rowIndex, j)); } result.add(cellResult); } @@ -234,7 +240,28 @@ public class ExcelParseHelper { for (; rowIndex < rowCount; rowIndex++) { List cellResult = new ArrayList(); for (int j = 0; j < cellCount; j++) { - cellResult.add(ExcelSupport.getCellValue(sheet, rowIndex, j)); + cellResult.add(ExcelSupport.getCellValue(sheet,null, rowIndex, j)); + } + result.add(cellResult); + } + return result; + } + + /** + * 将sheet数据转为List + * @param rowIndex 从哪行开始解析 + * @param headerRowIndex 抽取列数的参考行 + * @return + */ + public static List> parse2List(Sheet sheet, FormulaEvaluator evaluator ,int rowIndex, int headerRowIndex) { + int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数 + int cellCount = sheet.getRow(headerRowIndex).getPhysicalNumberOfCells(); // 总列数 + + List> result = new ArrayList>(); + for (; rowIndex < rowCount; rowIndex++) { + List cellResult = new ArrayList(); + for (int j = 0; j < cellCount; j++) { + cellResult.add(ExcelSupport.getCellValue(sheet,evaluator, rowIndex, j)); } result.add(cellResult); } @@ -310,4 +337,14 @@ public class ExcelParseHelper { } return val; } + + public static ExcelPreviewDTO preview(InputStream fileInputStream, int i, String excelTypeXlsx) { + Workbook workbook = ExcelSupport.parseFile(fileInputStream, EXCEL_TYPE_XLSX); + // 创建一个公式求值器对象 + 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(); + } } diff --git a/src/com/engine/salary/util/excel/ExcelPreviewDTO.java b/src/com/engine/salary/util/excel/ExcelPreviewDTO.java new file mode 100644 index 000000000..69135fbf0 --- /dev/null +++ b/src/com/engine/salary/util/excel/ExcelPreviewDTO.java @@ -0,0 +1,18 @@ +package com.engine.salary.util.excel; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class ExcelPreviewDTO { + private List headers; + + private List> list; +} diff --git a/src/com/engine/salary/util/excel/ExcelSupport.java b/src/com/engine/salary/util/excel/ExcelSupport.java index a13341a87..8ba0aa7f6 100644 --- a/src/com/engine/salary/util/excel/ExcelSupport.java +++ b/src/com/engine/salary/util/excel/ExcelSupport.java @@ -50,13 +50,34 @@ public class ExcelSupport { } } + /** + * 解析文件,获取单个sheet + * + * @return sheet + */ + public static Workbook parseFile(InputStream ins, String fileName) { + Workbook workBook = null; + try { + if (fileName.endsWith(EXCEL_TYPE_XLSX)) { + workBook = new XSSFWorkbook(new BufferedInputStream(ins)); + } else if (fileName.endsWith(EXCEL_TYPE_XLS)) { + workBook = new HSSFWorkbook(new BufferedInputStream(ins)); + } else { + throw new IllegalArgumentException("File format error! Only xlsx and xls types are supported"); + } + return workBook; + } catch (Exception e) { + throw new IllegalArgumentException(e); + } + } + /** * 解析文件,获取单个sheet * * @param sheetIndex sheet下标,从0开始 * @return sheet */ - public static Sheet parseFile(InputStream ins, int sheetIndex,String fileName) { + public static Sheet parseFile(InputStream ins, int sheetIndex, String fileName) { Workbook workBook = null; try { if (fileName.endsWith(EXCEL_TYPE_XLSX)) { @@ -93,16 +114,16 @@ public class ExcelSupport { /** - * 返回指定单元格的数据 + * 返回指定单元格的数据,支持Vlookup * * @param sheet 指定sheet * @param rowIndex 第几行,从0开始 * @param cellIndex 第几列,从0开始 * @return 值 */ - public static String getCellValue(Sheet sheet, int rowIndex, int cellIndex) { + public static String getCellValue(Sheet sheet, FormulaEvaluator evaluator, int rowIndex, int cellIndex) { Validate.notNull(sheet.getRow(rowIndex), "Line %s is empty and cannot be resolved", rowIndex); - return getCellValue(sheet.getRow(rowIndex).getCell(cellIndex)); + return getCellValue(sheet.getRow(rowIndex).getCell(cellIndex), evaluator); } @@ -111,7 +132,7 @@ public class ExcelSupport { /** * 格式化解析的数据 */ - public static String getCellValue(Cell cell) { + public static String getCellValue(Cell cell, FormulaEvaluator evaluator) { String cellValue = ""; if (cell != null) { switch (cell.getCellType()) { @@ -131,7 +152,15 @@ public class ExcelSupport { cellValue = String.valueOf(cell.getBooleanCellValue()); break; case FORMULA: // 公式类型 - cellValue = String.valueOf(cell.getCellFormula()); + if (evaluator != null) { + // 计算公式的值 + evaluator.evaluateFormulaCell(cell); + // 获取公式计算后的值 + cellValue = cell.getNumericCellValue() + ""; + } else { + cellValue = String.valueOf(cell.getCellFormula()); + } + break; case BLANK: // 空白类型 cellValue = ""; diff --git a/src/com/engine/salary/web/SpecialAddDeductionController.java b/src/com/engine/salary/web/SpecialAddDeductionController.java index 891f48d91..da67a4677 100644 --- a/src/com/engine/salary/web/SpecialAddDeductionController.java +++ b/src/com/engine/salary/web/SpecialAddDeductionController.java @@ -8,6 +8,7 @@ import com.engine.salary.entity.datacollection.param.SpecialAddDeductionParam; import com.engine.salary.entity.datacollection.param.SpecialAddDeductionQueryParam; import com.engine.salary.entity.datacollection.param.SpecialAddDeductionRecordDeleteParam; import com.engine.salary.util.ResponseResult; +import com.engine.salary.util.excel.ExcelPreviewDTO; import com.engine.salary.util.page.PageInfo; import com.engine.salary.wrapper.SpecialAddDeductionWrapper; import io.swagger.v3.oas.annotations.parameters.RequestBody; @@ -271,7 +272,7 @@ public class SpecialAddDeductionController { @Produces(MediaType.APPLICATION_JSON) public String preview(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SpecialAddDeductionImportParam importParam) { User user = HrmUserVarify.getUser(request, response); - return new ResponseResult>(user).run(getSpecialAddDeductionWrapper(user)::preview, importParam); + return new ResponseResult(user).run(getSpecialAddDeductionWrapper(user)::preview, importParam); } @POST diff --git a/src/com/engine/salary/wrapper/SpecialAddDeductionWrapper.java b/src/com/engine/salary/wrapper/SpecialAddDeductionWrapper.java index c5e571950..1e2586096 100644 --- a/src/com/engine/salary/wrapper/SpecialAddDeductionWrapper.java +++ b/src/com/engine/salary/wrapper/SpecialAddDeductionWrapper.java @@ -18,6 +18,7 @@ import com.engine.salary.service.impl.SalaryEmployeeServiceImpl; import com.engine.salary.service.impl.SpecialAddDeductionServiceImpl; import com.engine.salary.service.impl.TaxAgentServiceImpl; import com.engine.salary.util.SalaryI18nUtil; +import com.engine.salary.util.excel.ExcelPreviewDTO; import com.engine.salary.util.page.PageInfo; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import weaver.hrm.User; @@ -154,7 +155,7 @@ public class SpecialAddDeductionWrapper extends Service { /** * 预览 */ - public Map preview(SpecialAddDeductionImportParam importParam){ + public ExcelPreviewDTO preview(SpecialAddDeductionImportParam importParam){ return getSpecialAddDeductionService(user).preview(importParam); }