POI导出:
使用domain校验
1.依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<!--xlsx(07)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
前端:
<div id="btgp1" align="left">
<button id="btnTest" label="选择报盘" onClick='upload()'></button>
<input name="file" type="file" id="uploadFile" style="display: none" />
</div>
js代码:
var a= ""
function upload(b) {
a = b
$("#uploadFile").click()
}
var excelAae707;
var fileList=[] //上传材料数组 按顺序存储
$("#uploadFile").change(function () {
//初始化个人信息
const userInfo = JSON.parse(localStorage.getItem("account"))
//拿文件数据
var file = document.getElementById("uploadFile").files[0]
console.log("文件",document.getElementById("uploadFile").files[0])
var formData = new FormData();
var sysCode = 'ldgx'
formData.append("file",file)
formData.append("sysCode",sysCode)
formData.append("aab001", userInfo.aab001)
var columnKeys= [//用于封装domain
"htType",
"aac003",
"aac147",
"aac058",
"abc336",
"aac004",
"aae005",
"aac009",
"aae007",
"aac046",
"aac010",
"abc388",
"abc389",
"abc391",
"abc408",
"abc411",
"abc412",
"abc447",
"abc448",
"abc446",
"abc403",
"aab004",
"abc407",
"abc402",
"abc477",
"abc421",
"abc413",
"abc409",
"abc417",
"abc016",
"abc418",
"abc484",
"abc485",
"abc483",
"abc427",
"abc438",
]
let jsonArr = JSON.stringify(columnKeys);
formData.append("columnKeys",jsonArr)
//框架自带方法,看着改
Base.setDisabled("btnTest")
Base.msgTopTip("数据正在较验中,请稍等!","warn");
$.ajax({
url: '${rootPath}/api/base/excelCheck.do',
type: 'POST',
data: formData,
//文件上传时必须配置的参数
contentType: false,
processData: false,
dataType: "json",//返回的数据格式
success : function(data){ // 服务器成功响应处理函数
if (data.success){
//console.log("这里",data.resultData.msg)
var resultMsg=data.resultData.msg;
//var file1 = data.resultData.list[0]//上传材料返回值
}
},
error : function(data){// 服务器响应失败处理函数
$(a).prev().children("span").text("上传失败")
}
})
$("#uploadFile").val("")
})
后台代码:
这里会根据非空字段将读取到空白的excel数据去掉。
@PostMapping("/excelCheck.do")
public ReturnEnum excelCheck(@RequestPart("file") MultipartFile file) throws Exception {
if (file == null) {
throw new AppException("未获取到上传文件");
}
String originalFilename = file.getOriginalFilename();
if (originalFilename.lastIndexOf(".xls") == -1) {
throw new AppException("只能上传excel文件");
}
TerminalDto dto = getDto();
List<Map> excelData = null;
List<String> columnKeys = JSONObject.parseArray((String) dto.get("columnKeys"), String.class);
//非空字段
String checkId = "aac147";
try (InputStream is = file.getInputStream()) {
excelData = ExcelReadUtil.readExcel(is, originalFilename, columnKeys, 0, Integer.valueOf(1), null, null, null);
} catch (Exception e) {
logger.error("excel解析失败", e);
throw new AppException("excel数据解析失败");
}
if (ValidateUtil.isEmpty(excelData)) {
throw new AppException("报盘文件解析后数据长度为0,请检查格式是否正确");
}
while (excelData.size() > 0 && ValidateUtil.isEmpty(excelData.get(excelData.size() - 1).get(checkId))) {
excelData.remove(excelData.size() - 1);
}
if (ValidateUtil.isEmpty(excelData)) {
throw new AppException("报盘文件解析后数据长度为0,请检查格式是否正确");
}
Map map = dealExcelData(excelData, dto);
setData("msg", map);
return ReturnEnum.JSON;
}
数据校验方法:
将每条数据 封装成domain,通过domain进行非空或业务校验、
//数据校验
public Map dealExcelData(List<Map> excelData, TerminalDto dto) throws Exception {
Map checkResult;
List<HtqdAddBatchBean> list_HtqdAddBatchBean = null;
try {
list_HtqdAddBatchBean = JSON.parseArray(JSON.toJSONString(excelData), HtqdAddBatchBean.class);
} catch (Exception e) {
logger.error("转换对象失败", e);
throw new AppException("转换对象失败");
}
checkResult = checkUploadList(list_HtqdAddBatchBean, dto);
return checkResult;
}
public Map checkUploadList(List<HtqdAddBatchBean> HtqdAddBatchBean_list, TerminalDto dto) throws ParseException {
List<HtqdAddBatchBean> successList_HtqdInfo = new ArrayList();
List<HtqdAddBatchBean> errorList_HtqdInfo = new ArrayList();
String errorMsg = ""; //错误信息
Map checkResult = new HashMap();
//获取模板信息
// 1为全日制 2是劳务派遣
Map contractInfo = getContractInfo();
if (ValidateUtil.isEmpty(contractInfo)) {
checkResult.put("successList_HtqdInfo", successList_HtqdInfo);
checkResult.put("errorList_HtqdInfo", errorList_HtqdInfo);
return checkResult;
}
for (HtqdAddBatchBean htqdAddBatchBean : HtqdAddBatchBean_list) {
//校验必录项是否为空
if (!ValidateUtil.isEmpty(htqdAddBatchBean.checkNull())) {
errorMsg = htqdAddBatchBean.checkNull();
htqdAddBatchBean.setErrorMsg(errorMsg);
errorList_HtqdInfo.add(htqdAddBatchBean);
continue;
} else {
Map mbxx=getMbxx(contractInfo,htqdAddBatchBean.getHtType());
htqdAddBatchBean.setAbe401("1");//合同订立方式
htqdAddBatchBean.setAab301("");//户籍所在地id excel是没有的
htqdAddBatchBean.setAbc041("");//合同约定完成的工作任务
htqdAddBatchBean.setAbc445("");//用工单位id
htqdAddBatchBean.setAbc301("51" + System.currentTimeMillis() + "DZHT");//'51' + timestamp + 'DZHT'
htqdAddBatchBean.setAab001(dto.getAsString("aab001"));
htqdAddBatchBean.setAcc323(htqdAddBatchBean.getHtType());
htqdAddBatchBean.setAcc324("1");
htqdAddBatchBean.setAbat10(String.valueOf(mbxx.get("abat10")));//Abat10
htqdAddBatchBean.setTemplateId((String) mbxx.get("aae707"));
//如果校验通过,需要判断是否有重复数据
int i = checkRepeat(htqdAddBatchBean.getAac147());
if (i > 0) {
errorMsg = "该人员存在正在执行的合同,";
htqdAddBatchBean.setErrorMsg(errorMsg);
errorList_HtqdInfo.add(htqdAddBatchBean);
continue;
} else {
successList_HtqdInfo.add(htqdAddBatchBean);
}
}
}
checkResult.put("successList_HtqdInfo", successList_HtqdInfo);
checkResult.put("errorList_HtqdInfo", errorList_HtqdInfo);
return checkResult;
}
domain:
public class HtqdAddBatchBean {
private String htType;//合同类型
private String aac003;//姓名
private String aac147;//身份证号
private String aac058;//有效证件类型
private String abe401;//合同订立方式
private String abc336;//签订日期
private String aac004;//性别
private String aae005;//联系电话
private String aac009;//是否农业户口
private String aae007;//邮政编码
private String aac046;//常住地址
private String aac010;//户口所在地
private String abc388;//合同期限类型
private String abc389;//合同开始时间
private String abc391;//合同结束时间
private String abc408;//是否有试用期
private String abc411;//试用期开始时间
private String abc412;//试用期结束时间
private String abc447;//劳务派遣开始日期
private String abc448;//劳务派遣结束日期
private String abc446;//工作岗位
private String abc403;//工作区域或工作地点
private String aab004;//用工单位
private String abc407;//工作标准
private String abc402;//工时类型
private String abc477;//其它休假制度
private String abc421;//工资发放日
private String abc413;//月工资
private String abc409;//试用期工资
private String abc417;//待工生活费
private String abc016;//月平均工资
private String abc418;//工资的其他约定
private String abc484;//免费体检期限
private String abc485;//竞业、培训协议
private String abc483;//补充养老、医疗保险
private String abc427;//其他福利待遇说明
private String abc438;//双方约定的其他内容
private String errorMsg; //错误信息
private String aab301;//户口所在地id
private String abc041;//合同约定完成的工作任务
private String abc445;//用工单位id
private String abct10;//是否保存
private String[] fileList;//文件
private String abc301;//'51' + timestamp + 'DZHT'
private String aab001; //userInfo.aab001
private String acc324;//模板信息 1
private String acc323;//就是模板的aba495
private String abat10;
private String templateId;//aae707
//各种get和set方法省略。。。。。
//这个checkNull 方法就是用来判空的和业务校验
public String checkNull() throws ParseException {
String errorMessage = "";
if (ValidateUtil.isEmpty(this.getHtType())) {
errorMessage += "合同模板类型为空,";
} else {
this.setHtType(this.getHtType().split("-")[0]);
}
if (ValidateUtil.isEmpty(this.getAac003())) {
errorMessage += "姓名为空,";
}
if (ValidateUtil.isEmpty(this.getAac147())) {
errorMessage += "身份证号为空,";
}
if (ValidateUtil.isEmpty(this.getAac058())) {
errorMessage += "有效证件类型为空,";
} else {
this.setAac058(this.getAac058().split("-")[0]);
}
/* if (ValidateUtil.isEmpty(this.getAbe401())) {
errorMessage += "合同订立方式为空,";
} else {
this.setAbe401(this.getAbe401().split("-")[0]);
}*/
if (ValidateUtil.isEmpty(this.getAbc336())) {
errorMessage += "签订日期为空,";
}
if (ValidateUtil.isEmpty(this.getAac004())) {
errorMessage += "性别为空,";
} else {
this.setAac004(this.getAac004().split("-")[0]);
}
if (!ValidateUtil.isEmpty(this.getAac009())) {//是否农业户口
this.setAac009(this.getAac009().split("-")[0]);
}
if (ValidateUtil.isEmpty(this.getAae005())) {
errorMessage += "联系电话为空,";
}
if (ValidateUtil.isEmpty(this.getAae007())) {
errorMessage += "邮政编码为空,";
}
if (ValidateUtil.isEmpty(this.getAac046())) {
errorMessage += "常住地址为空,";
}
if (ValidateUtil.isEmpty(this.getAac010())) {
errorMessage += "户口所在地为空,";
}
if (ValidateUtil.isEmpty(this.getAbc388())) {
errorMessage += "合同期限类型为空,";
} else {
this.setAbc388(this.getAbc388().split("-")[0]);
}
if (ValidateUtil.isEmpty(this.getAbc389())) {
errorMessage += "合同开始时间为空,";
}
if (ValidateUtil.isEmpty(this.getAbc391())) {
errorMessage += "合同结束时间为空,";
}
if (ValidateUtil.isEmpty(this.getAbc408())) {
errorMessage += "是否有试用期为空,";
} else {
String abc408 = this.getAbc408().split("-")[0];
if (("1").equals(abc408)) {//1:是 0 否 如果为是,则必须填试用期开始时间和结束时间
if (ValidateUtil.isEmpty(this.getAbc411())) {
errorMessage += "试用期开始时间为空,";
}
if (ValidateUtil.isEmpty(this.getAbc412())) {
errorMessage += "试用期结束时间为空,";
}
}
this.setAbc408(this.getAbc408().split("-")[0]);
}
if (ValidateUtil.isEmpty(this.getAbc447())) {
errorMessage += "劳务派遣开始日期为空,";
}
if (ValidateUtil.isEmpty(this.getAbc448())) {
errorMessage += "劳务派遣结束日期为空,";
}
if (ValidateUtil.isEmpty(this.getAbc446())) {
errorMessage += "工作岗位为空,";
}
if (ValidateUtil.isEmpty(this.getAbc403())) {
errorMessage += "工作区域或工作地点为空,";
}
if (ValidateUtil.isEmpty(this.getAab004())) {
errorMessage += "用工单位为空,";
}
if (ValidateUtil.isEmpty(this.getAbc407())) {
errorMessage += "工作标准为空,";
}
if (ValidateUtil.isEmpty(this.getAbc402())) {
errorMessage += "工时类型为空,";
} else {
this.setAbc402(this.getAbc402().split("-")[0]);
}
if (ValidateUtil.isEmpty(this.getAbc421())) {
errorMessage += "工资发放日为空,";
}
if (ValidateUtil.isEmpty(this.getAbc413())) {
errorMessage += "月工资为空,";
} else {
this.setAbc413(getDecimal(this.getAbc413()));
}
if (ValidateUtil.isEmpty(this.getAbc409())) {
errorMessage += "试用期工资为空,";
} else {
this.setAbc409(getDecimal(this.getAbc409()));
}
if (!ValidateUtil.isEmpty(this.getAbc417())) {//待工生活费
this.setAbc417(getDecimal(this.getAbc417()));
}
if (!ValidateUtil.isEmpty(this.getAbc016())) {//月平均工资
this.setAbc016(getDecimal(this.getAbc016()));
}
if (!ValidateUtil.isEmpty(this.getAbc484())) {//免费体检期限
this.setAbc484(this.getAbc484().split("-")[0]);
}
if (!ValidateUtil.isEmpty(this.getAbc485())) {//竞业、培训协议
this.setAbc485(this.getAbc485().split("-")[0]);
}
//合同开始日期 结束日期
if (!ValidateUtil.isEmpty(this.getAbc389()) && !ValidateUtil.isEmpty(this.getAbc391())
&& Integer.valueOf(this.getAbc389().replaceAll("-", "")) > Integer.valueOf(this.getAbc391().replaceAll("-", ""))) {
errorMessage += "合同开始时间不能晚于结束时间,";
}
//是否有试用期,如果有,需要填试用期开始时间和结束时间
if (!ValidateUtil.isEmpty(this.getAbc408())) {
String acb408 = this.getAbc408().split("-")[0];//1:是 0:否
if ("1".equals(acb408)) {
//试用期开始时间 试用期结束时间
if (!ValidateUtil.isEmpty(this.getAbc411()) && !ValidateUtil.isEmpty(this.getAbc412())
&& Integer.valueOf(this.getAbc411().replaceAll("-", "")) > Integer.valueOf(this.getAbc412().replaceAll("-", ""))) {
errorMessage += "试用期开始时间不能晚于试用期结束时间,";
}
}
}
//劳务派遣开始日期 劳务派遣结束日期
if (!ValidateUtil.isEmpty(this.getAbc447()) && !ValidateUtil.isEmpty(this.getAbc448())
&& Integer.valueOf(this.getAbc447().replaceAll("-", "")) > Integer.valueOf(this.getAbc448().replaceAll("-", ""))) {
errorMessage += "劳务派遣开始日期不能晚于劳务派遣结束日期,";
}
return errorMessage;
}
//保留2位小数
public String getDecimal(String a) {
Double d = Double.parseDouble(a);
DecimalFormat df = new DecimalFormat("0.00");
String s = df.format(d);
return s;
}
}
循环判断每条数据的空和业务校验
最后将校验成功和失败的数据放入
successList 和 errorList,返给页面显示就行了。
第一次 通过domain去校验导入的数据,记录一下。。