easypoi导入的另外一种方式

easypoi导入的另外一种方式

1. 引入pom依赖
	<!--easypoi  start-->
		<dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-base</artifactId>
			<version>3.2.0</version>
		</dependency>

		<dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-web</artifactId>
			<version>3.2.0</version>
		</dependency>

		<dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-annotation</artifactId>
			<version>3.2.0</version>
		</dependency>
		<!--easypoi end-->

2. 导入学生实体类 
	@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class ImportStudent {

    @Excel(name = "账号",width = 25, isImportField = "true_st")
    private String loginName;

    @Excel(name = "用户名",width = 25, isImportField = "true_st")
    private String studentName;

    @Excel(name = "班级",width = 25, isImportField = "true_st")
    private String clazzName;

    @Excel(name = "最后一次登录时间",width = 35,exportFormat = "yyyy-MM-dd HH:mm:ss", isImportField = "true_st")
    private Date lastLoginTime;
}
3. 控制器方法
	@ApiOperation(value="教师---导入学生", notes="教师---导入学生")
    @PostMapping("/importStudent")
    @Transactional
    public Object importStudent(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return R.error("文件不能为空");
        }
        ImportParams params = new ImportParams();
        List<ImportStudent> list = new ArrayList<ImportStudent>();
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_name","student");
        Role role = roleService.getOne(queryWrapper);
        List<Long> roleIds = new ArrayList<>();
        if (role != null) {
            roleIds.add(role.getRoleId());
        }
        try {
            R infos = null;
            list = ExcelImportUtil.importExcel(file.getInputStream(), ImportStudent.class, params);
            if (list.size() > 0){
                for (ImportStudent importStudent:list) {
                    //sha256加密
                    String salt = RandomStringUtils.randomAlphanumeric(20);
                    String pwd = new Sha256Hash("123456", salt).toHex();
                    User user = new User().setPassword(pwd).setCreateUserId(this.getUserId()).setLoginErrorCount(0)
                            .setLastLoginTime(new Date()).setSalt(salt).setStatus(1).setUserName(importStudent.getStudentName())
                            .setLoginName(importStudent.getLoginName())
                            .setRoleIdList(roleIds);
                    R r = this.save(user);
                    if (!r.get("code").equals(200)){
                        infos = r;
                        return infos;
                    }
                    QueryWrapper<Clazz> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("clazz_name",importStudent.getClazzName());
                    Clazz clazz = clazzService.getOne(queryWrapper1);
                    if (clazz != null){
                        UserClazz userClazz = new UserClazz().setClazzId(clazz.getId()).setUserId(user.getId());
                        userClazzService.save(userClazz);
                    }

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
	

你可能感兴趣的:(springboot,poi)