| | |
| | | package com.whyc.controller; |
| | | |
| | | import cn.afterturn.easypoi.excel.ExcelImportUtil; |
| | | import cn.afterturn.easypoi.excel.entity.ImportParams; |
| | | import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.api.R; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | |
| | | import com.whyc.service.JobService; |
| | | import com.whyc.service.LoginService; |
| | | import com.whyc.service.UserService; |
| | | import com.whyc.util.UserExcelVerify; |
| | | import io.swagger.annotations.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | |
| | | |
| | | @Resource |
| | | private JobService jobService; |
| | | |
| | | @Resource |
| | | private UserExcelVerify userExcelVerify; |
| | | |
| | | @PostMapping |
| | | @ApiOperation(value = "添加用户") |
| | |
| | | return jobService.delete(id); |
| | | } |
| | | |
| | | |
| | | @PostMapping("/importUserExcel") |
| | | @ApiOperation(value = "导入用户数据") |
| | | public Response importExcel2(@RequestParam("file") MultipartFile file) { |
| | | Response response = new Response(); |
| | | ImportParams importParams = new ImportParams(); |
| | | // 数据处理 |
| | | importParams.setTitleRows(0); //设置标题第1行开始 |
| | | importParams.setHeadRows(1); //设置开始行从第3行开始 |
| | | // 需要验证 |
| | | importParams.setNeedVerify(true); |
| | | //设及一个自定义校验 (自定义校验名字不可重复) 使用注解注入 |
| | | //UserExcelVerify userExcelVerify = new UserExcelVerify(); |
| | | importParams.setVerifyHandler(userExcelVerify); |
| | | try { |
| | | ExcelImportResult<User> result = ExcelImportUtil.importExcelMore(file.getInputStream(), User.class, |
| | | importParams); |
| | | List<User> successList = result.getList(); |
| | | //失败结果集 |
| | | List<User> failList = result.getFailList(); |
| | | /*failList.forEach(user -> { |
| | | System.out.println("验证失败的信息:"+user+userExcelVerify.verifyHandler(user).getMsg()); |
| | | }); |
| | | successList.forEach(x->{ |
| | | System.out.println("通过验证的数据:"+x.toString()); |
| | | });*/ |
| | | String msg = ""; |
| | | for (User entity : result.getFailList()) { |
| | | msg += "第" + entity.getRowNum() + "行的错误是:" + entity.getErrorMsg()+";\n"; |
| | | //System.out.println(msg); |
| | | } |
| | | if (!successList.isEmpty()){ |
| | | boolean success = userService.addBatch(successList); |
| | | if (success && failList.isEmpty()){ |
| | | response.set(1,"成功导入"+successList.size()+"条数据;"); |
| | | } |
| | | }else{ |
| | | response.set(1,failList,"成功导入"+successList.size()+"条数据"+";导入失败"+failList.size()+"条数据;\n"+msg); |
| | | } |
| | | |
| | | |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | response.setMsg(0,"导入失败"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | response.setMsg(0,"导入失败"); |
| | | } |
| | | return response; |
| | | } |
| | | |
| | | } |