public ResultVO importFinacePayrollExcel(MultipartFile file, HttpServletRequest req, SysUser user) {
try {
if (!file.getOriginalFilename().endsWith(".xls") && !file.getOriginalFilename().endsWith(".xlsx")) {
throw new IOException("导入文件格式错误!");
}
ImportParams params = new ImportParams();
// 固定模板头部标题
params.setNeedVerfiy(true);
ExcelImportResult<FinacePayrollVo> result = null;
try {
//主要部分
result = ExcelImportUtil.importExcelMore(file.getInputStream(),
FinacePayrollVo.class, params);
} catch (Exception e) {
e.printStackTrace();
return ResultVO.ERROR("数据校验异常", null);
}
if (result.isVerfiyFail()) {
StringJoiner errorMsgs = new StringJoiner("," + "<br/>");
for (FinacePayrollVo vo : result.getFailList()) {
errorMsgs.add("第" + vo.getRowNum() + "行的错误是:" + vo.getErrorMsg());
}
return ResultVO.FAIL(errorMsgs.toString(), null);
} else if (result.getList() != null && !result.getList().isEmpty()) {
List<FinacePayrollVo> successlist = result.getList();
if (successlist != null && !successlist.isEmpty()) {
// 具体数据导入,根据情况编写
return importFinacePayrollFile(file, successlist, user);
}
}else {
return ResultVO.FAIL("导入表中数据异常,或没有数据", null);
}
return ResultVO.SUCCESS();
} catch (Exception e) {
e.printStackTrace();
return ResultVO.ERROR();
}
}
/-------------------------------实体---------------------------------/
@Data
@NoArgsConstructor
public class FinacePayrollVo implements IExcelDataModel, IExcelModel {
private int rowNum;
private String errorMsg;
@Excel(name = "人员代码" )
@NotBlank(message = "[人员代码]不能为空")
private String empNo;
@Excel(name = "姓名")
@NotBlank(message = "[姓名]不能为空")
private String empName;
@Excel(name = "年月")
@NotBlank(message = "[年月]不能为空")
private String payrollMonth;
@Excel(name = "部门代码")
private Integer empDeptId;
@Excel(name = "部门名称")
private String empDeptName;
}
固定模板头导入
最新推荐文章于 2022-04-24 10:50:47 发布