| | |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-aop</artifactId> |
| | | </dependency> |
| | | <!--导入导出--> |
| | | <dependency> |
| | | <groupId>cn.afterturn</groupId> |
| | | <artifactId>easypoi-spring-boot-starter</artifactId> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>com.google.guava</groupId> |
| | | <artifactId>guava</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | <version>4.1.2</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>commons-fileupload</groupId> |
| | | <artifactId>commons-fileupload</artifactId> |
| | | <version>1.3.1</version> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | <build> |
| | |
| | | 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; |
| | | } |
| | | |
| | | } |
| | |
| | | package com.whyc.pojo; |
| | | |
| | | import cn.afterturn.easypoi.excel.annotation.Excel; |
| | | import cn.afterturn.easypoi.excel.annotation.ExcelTarget; |
| | | import cn.afterturn.easypoi.handler.inter.IExcelDataModel; |
| | | import cn.afterturn.easypoi.handler.inter.IExcelModel; |
| | | import com.baomidou.mybatisplus.annotation.TableField; |
| | | import com.baomidou.mybatisplus.annotation.TableName; |
| | | import com.fasterxml.jackson.annotation.JsonFormat; |
| | | import io.swagger.annotations.ApiModel; |
| | |
| | | import lombok.*; |
| | | import org.apache.ibatis.type.Alias; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.Date; |
| | | @ExcelTarget("User") |
| | | @Alias("User") |
| | | @TableName( schema = "`db_3.5mw_web`",value = "tb_user") |
| | | @Data |
| | | @ApiModel |
| | | public class User { |
| | | public class User implements IExcelDataModel, IExcelModel { |
| | | @TableField(exist = false) |
| | | private Integer rowNum; |
| | | @TableField(exist = false) |
| | | private String errorMsg; |
| | | |
| | | private Integer id; |
| | | @ApiModelProperty("用户账号") |
| | | @Excel(name = "账号",width = 25,orderNum = "1",isImportField = "true_st") |
| | | private String name; |
| | | @ApiModelProperty("用户密码") |
| | | @Excel(name = "密码",width = 25,orderNum = "2",isImportField = "true_st") |
| | | private String password; |
| | | @ApiModelProperty("真实姓名") |
| | | @ApiModelProperty("姓名") |
| | | @Excel(name = "姓名",width = 25,orderNum = "3",isImportField = "true_st") |
| | | private String realName; |
| | | @ApiModelProperty("性别") |
| | | @Excel(name = "性别",replace = {"男_0","女_1"},width = 25,orderNum = "4",isImportField = "true_st") |
| | | private Integer sex; |
| | | @ApiModelProperty("手机号") |
| | | @Excel(name = "手机号",width = 25,orderNum = "5",isImportField = "true_st") |
| | | private String phone; |
| | | @ApiModelProperty("邮箱地址") |
| | | @Excel(name = "邮箱地址",width = 25,orderNum = "6",isImportField = "true_st") |
| | | private String email; |
| | | @ApiModelProperty("部门") |
| | | @Excel(name = "部门",width = 25,orderNum = "7",isImportField = "true_st") |
| | | private String dept; |
| | | @ApiModelProperty("职务") |
| | | @Excel(name = "职务",width = 25,orderNum = "8",isImportField = "true_st") |
| | | private String job; |
| | | @ApiModelProperty("加密因子") |
| | | private String salt; |
| | | @ApiModelProperty("状态:0正常,1冻结,2删除") |
| | | //@Excel(name = "状态",width = 25,orderNum = "9",isImportField = "true_st") |
| | | private Integer state; |
| | | @JsonFormat(locale = "zh",timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss") |
| | | private Date createTime; |
| | |
| | | this.name = name; |
| | | } |
| | | |
| | | public User(Integer id, String name, String password, String realName, Integer sex, String phone, String email, String dept, String job, String salt, Integer state, Date createTime) { |
| | | public User(int rowNum, String errorMsg, Integer id, String name, String password, String realName, Integer sex, String phone, String email, String dept, String job, String salt, Integer state, Date createTime) { |
| | | this.rowNum = rowNum; |
| | | this.errorMsg = errorMsg; |
| | | this.id = id; |
| | | this.name = name; |
| | | this.password = password; |
| | |
| | | this.state = state; |
| | | this.createTime = createTime; |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | return new Response<>().set(0); |
| | | } |
| | | } |
| | | public boolean addBatch(List<User> roles) { |
| | | return userMapper.insertBatchSomeColumn(roles)==roles.size(); |
| | | } |
| | | |
| | | |
| | | |
| | | public User getById(int id) { |
| | | return userMapper.selectById(id); |
New file |
| | |
| | | package com.whyc.util; |
| | | |
| | | import cn.afterturn.easypoi.excel.ExcelExportUtil; |
| | | import cn.afterturn.easypoi.excel.ExcelImportUtil; |
| | | import cn.afterturn.easypoi.excel.entity.ExportParams; |
| | | import cn.afterturn.easypoi.excel.entity.ImportParams; |
| | | import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.URLEncoder; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.NoSuchElementException; |
| | | |
| | | /** |
| | | * @ClassName: ExcelUtils |
| | | * @Description: excle工具类 |
| | | * @author 765199214 |
| | | * |
| | | */ |
| | | public class ExcelUtils { |
| | | /** |
| | | * excel 导出 |
| | | * |
| | | * @param list 数据 |
| | | * @param title 标题 |
| | | * @param sheetName sheet名称 |
| | | * @param pojoClass pojo类型 |
| | | * @param fileName 文件名称 |
| | | * @param isCreateHeader 是否创建表头 |
| | | * @param response |
| | | */ |
| | | public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) throws IOException { |
| | | ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF); |
| | | exportParams.setCreateHeadRows(isCreateHeader); |
| | | defaultExport(list, pojoClass, fileName, response, exportParams); |
| | | } |
| | | |
| | | /** |
| | | * excel 导出 |
| | | * |
| | | * @param list 数据 |
| | | * @param title 标题 |
| | | * @param sheetName sheet名称 |
| | | * @param pojoClass pojo类型 |
| | | * @param fileName 文件名称 |
| | | * @param response |
| | | */ |
| | | public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException { |
| | | defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, ExcelType.XSSF)); |
| | | } |
| | | |
| | | /** |
| | | * excel 导出 |
| | | * |
| | | * @param list 数据 |
| | | * @param pojoClass pojo类型 |
| | | * @param fileName 文件名称 |
| | | * @param response |
| | | * @param exportParams 导出参数 |
| | | */ |
| | | public static void exportExcel(List<?> list, Class<?> pojoClass, String fileName, ExportParams exportParams, HttpServletResponse response) throws IOException { |
| | | defaultExport(list, pojoClass, fileName, response, exportParams); |
| | | } |
| | | |
| | | /** |
| | | * excel 导出 |
| | | * |
| | | * @param list 数据 |
| | | * @param fileName 文件名称 |
| | | * @param response |
| | | */ |
| | | public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException { |
| | | defaultExport(list, fileName, response); |
| | | } |
| | | |
| | | /** |
| | | * 默认的 excel 导出 |
| | | * |
| | | * @param list 数据 |
| | | * @param pojoClass pojo类型 |
| | | * @param fileName 文件名称 |
| | | * @param response |
| | | * @param exportParams 导出参数 |
| | | */ |
| | | private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException { |
| | | Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list); |
| | | downLoadExcel(fileName, response, workbook); |
| | | } |
| | | |
| | | /** |
| | | * 默认的 excel 导出 |
| | | * |
| | | * @param list 数据 |
| | | * @param fileName 文件名称 |
| | | * @param response |
| | | */ |
| | | private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException { |
| | | Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF); |
| | | downLoadExcel(fileName, response, workbook); |
| | | } |
| | | |
| | | /** |
| | | * 下载 |
| | | * |
| | | * @param fileName 文件名称 |
| | | * @param response |
| | | * @param workbook excel数据 |
| | | */ |
| | | private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException { |
| | | try { |
| | | response.setCharacterEncoding("UTF-8"); |
| | | response.setHeader("content-Type", "application/vnd.ms-excel"); |
| | | response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + "." + ExcelTypeEnum.XLSX.getValue(), "UTF-8")); |
| | | workbook.write(response.getOutputStream()); |
| | | } catch (Exception e) { |
| | | throw new IOException(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * excel 导入 |
| | | * |
| | | * @param filePath excel文件路径 |
| | | * @param titleRows 标题行 |
| | | * @param headerRows 表头行 |
| | | * @param pojoClass pojo类型 |
| | | * @param <T> |
| | | * @return |
| | | */ |
| | | public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException { |
| | | if (StringUtils.isBlank(filePath)) { |
| | | return null; |
| | | } |
| | | ImportParams params = new ImportParams(); |
| | | params.setTitleRows(titleRows); |
| | | params.setHeadRows(headerRows); |
| | | params.setNeedSave(true); |
| | | params.setSaveUrl("/excel/"); |
| | | try { |
| | | return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); |
| | | } catch (NoSuchElementException e) { |
| | | throw new IOException("模板不能为空"); |
| | | } catch (Exception e) { |
| | | throw new IOException(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * excel 导入 |
| | | * |
| | | * @param file excel文件 |
| | | * @param pojoClass pojo类型 |
| | | * @param <T> |
| | | * @return |
| | | */ |
| | | public static <T> List<T> importExcel(MultipartFile file, Class<T> pojoClass) throws IOException { |
| | | return importExcel(file, 1, 1, pojoClass); |
| | | } |
| | | |
| | | /** |
| | | * excel 导入 |
| | | * |
| | | * @param file excel文件 |
| | | * @param titleRows 标题行 |
| | | * @param headerRows 表头行 |
| | | * @param pojoClass pojo类型 |
| | | * @param <T> |
| | | * @return |
| | | */ |
| | | public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException { |
| | | return importExcel(file, titleRows, headerRows, false, pojoClass); |
| | | } |
| | | |
| | | /** |
| | | * excel 导入 |
| | | * |
| | | * @param file 上传的文件 |
| | | * @param titleRows 标题行 |
| | | * @param headerRows 表头行 |
| | | * @param needVerfiy 是否检验excel内容 |
| | | * @param pojoClass pojo类型 |
| | | * @param <T> |
| | | * @return |
| | | */ |
| | | public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, boolean needVerfiy, Class<T> pojoClass) throws IOException { |
| | | if (file == null) { |
| | | return null; |
| | | } |
| | | try { |
| | | return importExcel(file.getInputStream(), titleRows, headerRows, needVerfiy, pojoClass); |
| | | } catch (Exception e) { |
| | | throw new IOException(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * excel 导入 |
| | | * |
| | | * @param inputStream 文件输入流 |
| | | * @param titleRows 标题行 |
| | | * @param headerRows 表头行 |
| | | * @param needVerfiy 是否检验excel内容 |
| | | * @param pojoClass pojo类型 |
| | | * @param <T> |
| | | * @return |
| | | */ |
| | | public static <T> List<T> importExcel(InputStream inputStream, Integer titleRows, Integer headerRows, boolean needVerfiy, Class<T> pojoClass) throws IOException { |
| | | if (inputStream == null) { |
| | | return null; |
| | | } |
| | | ImportParams params = new ImportParams(); |
| | | params.setTitleRows(titleRows); |
| | | params.setHeadRows(headerRows); |
| | | params.setSaveUrl("/excel/"); |
| | | params.setNeedSave(true); |
| | | //params.setNeedVerify(needVerfiy); |
| | | try { |
| | | return ExcelImportUtil.importExcel(inputStream, pojoClass, params); |
| | | } catch (NoSuchElementException e) { |
| | | throw new IOException("excel文件不能为空"); |
| | | } catch (Exception e) { |
| | | throw new IOException(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Excel 类型枚举 |
| | | */ |
| | | enum ExcelTypeEnum { |
| | | XLS("xls"), XLSX("xlsx"); |
| | | private String value; |
| | | |
| | | ExcelTypeEnum(String value) { |
| | | this.value = value; |
| | | } |
| | | |
| | | public String getValue() { |
| | | return value; |
| | | } |
| | | |
| | | public void setValue(String value) { |
| | | this.value = value; |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.whyc.util; |
| | | |
| | | import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult; |
| | | import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.mapper.UserMapper; |
| | | import com.whyc.pojo.User; |
| | | import com.whyc.service.UserBridgeService; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * @author 张辉 |
| | | * @since 2021/4/27 |
| | | */ |
| | | @Component |
| | | public class UserExcelVerify implements IExcelVerifyHandler<User> { |
| | | |
| | | @Resource |
| | | private UserMapper userMapper; |
| | | |
| | | @Resource |
| | | private UserBridgeService userBridgeService; |
| | | @Override |
| | | public ExcelVerifyHandlerResult verifyHandler(User user) { |
| | | //设置默认验证为true |
| | | ExcelVerifyHandlerResult excelVerifyHandlerResult = new ExcelVerifyHandlerResult(true); |
| | | Map<String,String> map = new HashMap<>(); |
| | | //验证数据库是否有此用户 |
| | | User userFound = userBridgeService.findPasswordAndSlatByUserName(user.getName()); |
| | | if (userFound.getId() != 0) { |
| | | excelVerifyHandlerResult.setSuccess(false); |
| | | map.put("用户校验错误信息","对不起,此用户已存在,请不要重复提交"); |
| | | } |
| | | user.setCreateTime(new Date()); |
| | | user.setState(0); |
| | | Map<String, String> encryptMap = DigestsUtil.encryptPassword(user.getPassword()); |
| | | user.setPassword(encryptMap.get("password")); |
| | | user.setSalt(encryptMap.get("salt")); |
| | | //给返回对象添加错误信息 |
| | | excelVerifyHandlerResult.setMsg(map.toString()); |
| | | return excelVerifyHandlerResult; |
| | | |
| | | } |
| | | } |