支持Vlookup

This commit is contained in:
钱涛 2024-08-16 12:19:18 +08:00
parent 7250ed3520
commit d23baeb940
7 changed files with 112 additions and 28 deletions

View File

@ -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<String, Object> preview(SpecialAddDeductionImportParam importParam);
ExcelPreviewDTO preview(SpecialAddDeductionImportParam importParam);
/**
* 导入数据

View File

@ -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<String, Object> preview(SpecialAddDeductionImportParam importParam) {
Map<String, Object> apidatas = new HashMap<String, Object>();
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<DataCollectionEmployee> 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()

View File

@ -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<String> cellResult = new ArrayList<String>();
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<List<String>> 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<List<String>> result = new ArrayList<List<String>>();
for (; rowIndex < rowCount; rowIndex++) {
List<String> cellResult = new ArrayList<String>();
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<String, Object> 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<String, Object> 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<String, Object> 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<String> cellResult = new ArrayList<String>();
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<String> cellResult = new ArrayList<String>();
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<List<String>> parse2List(Sheet sheet, FormulaEvaluator evaluator ,int rowIndex, int headerRowIndex) {
int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数
int cellCount = sheet.getRow(headerRowIndex).getPhysicalNumberOfCells(); // 总列数
List<List<String>> result = new ArrayList<List<String>>();
for (; rowIndex < rowCount; rowIndex++) {
List<String> cellResult = new ArrayList<String>();
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();
}
}

View File

@ -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<String> headers;
private List<List<String>> list;
}

View File

@ -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 = "";

View File

@ -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<SpecialAddDeductionImportParam, Map<String, Object>>(user).run(getSpecialAddDeductionWrapper(user)::preview, importParam);
return new ResponseResult<SpecialAddDeductionImportParam, ExcelPreviewDTO>(user).run(getSpecialAddDeductionWrapper(user)::preview, importParam);
}
@POST

View File

@ -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<String, Object> preview(SpecialAddDeductionImportParam importParam){
public ExcelPreviewDTO preview(SpecialAddDeductionImportParam importParam){
return getSpecialAddDeductionService(user).preview(importParam);
}