用户列表导入导出excel
This commit is contained in:
parent
e02950a1dc
commit
38c9297b2d
|
|
@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -40,7 +39,7 @@ public class SysLogLoginController {
|
||||||
|
|
||||||
@GetMapping("export")
|
@GetMapping("export")
|
||||||
@Operation(summary = "导出excel")
|
@Operation(summary = "导出excel")
|
||||||
public Result<Map<String, String>> export() throws IOException {
|
public Result<Map<String, String>> export() {
|
||||||
Map<String, String> map = sysLogLoginService.export();
|
Map<String, String> map = sysLogLoginService.export();
|
||||||
|
|
||||||
return Result.ok(map);
|
return Result.ok(map);
|
||||||
|
|
|
||||||
|
|
@ -19,9 +19,11 @@ import net.maku.system.vo.SysUserVO;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -137,4 +139,23 @@ public class SysUserController {
|
||||||
|
|
||||||
return Result.ok();
|
return Result.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("import")
|
||||||
|
@Operation(summary = "导入用户")
|
||||||
|
public Result<String> importExcel(@RequestParam("file") MultipartFile file) {
|
||||||
|
if (file.isEmpty()) {
|
||||||
|
return Result.error("请选择需要上传的文件");
|
||||||
|
}
|
||||||
|
sysUserService.importByExcel(file, passwordEncoder.encode("123456"));
|
||||||
|
|
||||||
|
return Result.ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("export")
|
||||||
|
@Operation(summary = "导出用户")
|
||||||
|
public Result<Map<String, String>> export() {
|
||||||
|
Map<String, String> map = sysUserService.export();
|
||||||
|
|
||||||
|
return Result.ok(map);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package net.maku.system.convert;
|
||||||
|
|
||||||
import net.maku.framework.security.user.UserDetail;
|
import net.maku.framework.security.user.UserDetail;
|
||||||
import net.maku.system.entity.SysUserEntity;
|
import net.maku.system.entity.SysUserEntity;
|
||||||
|
import net.maku.system.vo.SysUserExcelVO;
|
||||||
import net.maku.system.vo.SysUserVO;
|
import net.maku.system.vo.SysUserVO;
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
import org.mapstruct.factory.Mappers;
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
@ -23,4 +24,8 @@ public interface SysUserConvert {
|
||||||
|
|
||||||
List<SysUserVO> convertList(List<SysUserEntity> list);
|
List<SysUserVO> convertList(List<SysUserEntity> list);
|
||||||
|
|
||||||
|
List<SysUserExcelVO> convert2List(List<SysUserEntity> list);
|
||||||
|
|
||||||
|
List<SysUserEntity> convertListEntity(List<SysUserExcelVO> list);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,41 @@ package net.maku.system.enums;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 超级管理员枚举
|
* 超级管理员枚举
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public enum SuperAdminEnum {
|
public enum SuperAdminEnum {
|
||||||
YES(1),
|
/**
|
||||||
NO(0);
|
* 是
|
||||||
|
*/
|
||||||
|
YES(1, "是"),
|
||||||
|
/**
|
||||||
|
* 否
|
||||||
|
*/
|
||||||
|
NO(0, "否");
|
||||||
|
|
||||||
private final Integer value;
|
private final Integer value;
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public static String getNameByValue(int value) {
|
||||||
|
for (SuperAdminEnum s : SuperAdminEnum.values()) {
|
||||||
|
if (s.getValue() == value) {
|
||||||
|
return s.getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer getValueByName(String name) {
|
||||||
|
for (SuperAdminEnum s : SuperAdminEnum.values()) {
|
||||||
|
if (Objects.equals(s.getName(), name)) {
|
||||||
|
return s.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
package net.maku.system.enums;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户性别状态
|
||||||
|
*
|
||||||
|
* @author 阿沐 babamu@126.com
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum UserGenderEnum {
|
||||||
|
/**
|
||||||
|
* 男
|
||||||
|
*/
|
||||||
|
MAN(0, "男"),
|
||||||
|
/**
|
||||||
|
* 女
|
||||||
|
*/
|
||||||
|
WOMEN(1, "女"),
|
||||||
|
/**
|
||||||
|
* 未知
|
||||||
|
*/
|
||||||
|
UNKNOWN(2,"未知");
|
||||||
|
|
||||||
|
private final int value;
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public static String getNameByValue(int value) {
|
||||||
|
for (UserGenderEnum s : UserGenderEnum.values()) {
|
||||||
|
if (s.getValue() == value) {
|
||||||
|
return s.getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer getValueByName(String name) {
|
||||||
|
for (UserGenderEnum s : UserGenderEnum.values()) {
|
||||||
|
if (Objects.equals(s.getName(), name)) {
|
||||||
|
return s.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,8 @@ package net.maku.system.enums;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户状态
|
* 用户状态
|
||||||
*
|
*
|
||||||
|
|
@ -14,11 +16,30 @@ public enum UserStatusEnum {
|
||||||
/**
|
/**
|
||||||
* 停用
|
* 停用
|
||||||
*/
|
*/
|
||||||
DISABLE(0),
|
DISABLE(0, "停用"),
|
||||||
/**
|
/**
|
||||||
* 正常
|
* 正常
|
||||||
*/
|
*/
|
||||||
ENABLED(1);
|
ENABLED(1, "正常");
|
||||||
|
|
||||||
private final int value;
|
private final int value;
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public static String getNameByValue(int value) {
|
||||||
|
for (UserStatusEnum s : UserStatusEnum.values()) {
|
||||||
|
if (s.getValue() == value) {
|
||||||
|
return s.getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer getValueByName(String name) {
|
||||||
|
for (UserStatusEnum s : UserStatusEnum.values()) {
|
||||||
|
if (Objects.equals(s.getName(), name)) {
|
||||||
|
return s.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,6 @@ public interface SysLogLoginService extends BaseService<SysLogLoginEntity> {
|
||||||
* 导出登录日志
|
* 导出登录日志
|
||||||
*
|
*
|
||||||
* @return the map
|
* @return the map
|
||||||
* @throws IOException the io exception
|
|
||||||
*/
|
*/
|
||||||
Map<String, String> export() throws IOException;
|
Map<String, String> export();
|
||||||
}
|
}
|
||||||
|
|
@ -6,8 +6,10 @@ import net.maku.system.entity.SysUserEntity;
|
||||||
import net.maku.system.query.SysRoleUserQuery;
|
import net.maku.system.query.SysRoleUserQuery;
|
||||||
import net.maku.system.query.SysUserQuery;
|
import net.maku.system.query.SysUserQuery;
|
||||||
import net.maku.system.vo.SysUserVO;
|
import net.maku.system.vo.SysUserVO;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户管理
|
* 用户管理
|
||||||
|
|
@ -39,4 +41,18 @@ public interface SysUserService extends BaseService<SysUserEntity> {
|
||||||
*/
|
*/
|
||||||
PageResult<SysUserVO> roleUserPage(SysRoleUserQuery query);
|
PageResult<SysUserVO> roleUserPage(SysRoleUserQuery query);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量导入用户
|
||||||
|
*
|
||||||
|
* @param file excel文件
|
||||||
|
* @param password 密码
|
||||||
|
*/
|
||||||
|
void importByExcel(MultipartFile file, String password);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出用户信息表格
|
||||||
|
*
|
||||||
|
* @return map
|
||||||
|
*/
|
||||||
|
Map<String, String> export();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
import net.maku.framework.common.page.PageResult;
|
import net.maku.framework.common.page.PageResult;
|
||||||
import net.maku.framework.common.service.impl.BaseServiceImpl;
|
import net.maku.framework.common.service.impl.BaseServiceImpl;
|
||||||
import net.maku.framework.common.utils.AddressUtils;
|
import net.maku.framework.common.utils.AddressUtils;
|
||||||
|
|
@ -24,7 +25,6 @@ import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -78,18 +78,19 @@ public class SysLogLoginServiceImpl extends BaseServiceImpl<SysLogLoginDao, SysL
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, String> export() throws IOException {
|
@SneakyThrows
|
||||||
|
public Map<String, String> export() {
|
||||||
List<SysLogLoginEntity> list = list();
|
List<SysLogLoginEntity> list = list();
|
||||||
List<SysLogLoginVO> sysLogLoginVOS = SysLogLoginConvert.INSTANCE.convertList(list);
|
List<SysLogLoginVO> sysLogLoginVOS = SysLogLoginConvert.INSTANCE.convertList(list);
|
||||||
File file = File.createTempFile("log_excel", ".xlsx");
|
|
||||||
|
File file = File.createTempFile("system_login_log_excel", ".xlsx");
|
||||||
// 写入到文件
|
// 写入到文件
|
||||||
ExcelUtils.excelExport(SysLogLoginVO.class, file, sysLogLoginVOS);
|
ExcelUtils.excelExport(SysLogLoginVO.class, file, sysLogLoginVOS);
|
||||||
|
|
||||||
byte[] data = IoUtil.readBytes(Files.newInputStream(file.toPath()));
|
byte[] data = IoUtil.readBytes(Files.newInputStream(file.toPath()));
|
||||||
|
|
||||||
String path = storageService.getPath(file.getName());
|
String path = storageService.getPath(file.getName());
|
||||||
String url = storageService.upload(data, path);
|
String url = storageService.upload(data, path);
|
||||||
Map<String, String> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>(2);
|
||||||
map.put("path", url);
|
map.put("path", url);
|
||||||
map.put("filename", file.getName());
|
map.put("filename", file.getName());
|
||||||
return map;
|
return map;
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,17 @@
|
||||||
package net.maku.system.service.impl;
|
package net.maku.system.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.IoUtil;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
import net.maku.framework.common.constant.Constant;
|
import net.maku.framework.common.constant.Constant;
|
||||||
|
import net.maku.framework.common.excel.ExcelFinishCallBack;
|
||||||
import net.maku.framework.common.exception.ServerException;
|
import net.maku.framework.common.exception.ServerException;
|
||||||
import net.maku.framework.common.page.PageResult;
|
import net.maku.framework.common.page.PageResult;
|
||||||
import net.maku.framework.common.service.impl.BaseServiceImpl;
|
import net.maku.framework.common.service.impl.BaseServiceImpl;
|
||||||
|
import net.maku.framework.common.utils.ExcelUtils;
|
||||||
|
import net.maku.storage.service.StorageService;
|
||||||
import net.maku.system.convert.SysUserConvert;
|
import net.maku.system.convert.SysUserConvert;
|
||||||
import net.maku.system.dao.SysUserDao;
|
import net.maku.system.dao.SysUserDao;
|
||||||
import net.maku.system.entity.SysUserEntity;
|
import net.maku.system.entity.SysUserEntity;
|
||||||
|
|
@ -15,10 +21,14 @@ import net.maku.system.query.SysUserQuery;
|
||||||
import net.maku.system.service.SysUserPostService;
|
import net.maku.system.service.SysUserPostService;
|
||||||
import net.maku.system.service.SysUserRoleService;
|
import net.maku.system.service.SysUserRoleService;
|
||||||
import net.maku.system.service.SysUserService;
|
import net.maku.system.service.SysUserService;
|
||||||
|
import net.maku.system.vo.SysUserExcelVO;
|
||||||
import net.maku.system.vo.SysUserVO;
|
import net.maku.system.vo.SysUserVO;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -33,6 +43,7 @@ import java.util.Map;
|
||||||
public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {
|
public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {
|
||||||
private final SysUserRoleService sysUserRoleService;
|
private final SysUserRoleService sysUserRoleService;
|
||||||
private final SysUserPostService sysUserPostService;
|
private final SysUserPostService sysUserPostService;
|
||||||
|
private final StorageService storageService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PageResult<SysUserVO> page(SysUserQuery query) {
|
public PageResult<SysUserVO> page(SysUserQuery query) {
|
||||||
|
|
@ -131,7 +142,7 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUserEntit
|
||||||
@Override
|
@Override
|
||||||
public SysUserVO getByMobile(String mobile) {
|
public SysUserVO getByMobile(String mobile) {
|
||||||
SysUserEntity user = baseMapper.getByMobile(mobile);
|
SysUserEntity user = baseMapper.getByMobile(mobile);
|
||||||
|
|
||||||
return SysUserConvert.INSTANCE.convert(user);
|
return SysUserConvert.INSTANCE.convert(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -160,4 +171,47 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUserEntit
|
||||||
return new PageResult<>(SysUserConvert.INSTANCE.convertList(list), page.getTotal());
|
return new PageResult<>(SysUserConvert.INSTANCE.convertList(list), page.getTotal());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void importByExcel(MultipartFile file, String password) {
|
||||||
|
|
||||||
|
ExcelUtils.readAnalysis(file, SysUserExcelVO.class, new ExcelFinishCallBack<>() {
|
||||||
|
@Override
|
||||||
|
public void doAfterAllAnalysed(List<SysUserExcelVO> result) {
|
||||||
|
saveUser(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doSaveBatch(List<SysUserExcelVO> result) {
|
||||||
|
saveUser(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveUser(List<SysUserExcelVO> result) {
|
||||||
|
List<SysUserEntity> sysUserEntities = SysUserConvert.INSTANCE.convertListEntity(result);
|
||||||
|
sysUserEntities.forEach(user -> user.setPassword(password));
|
||||||
|
saveBatch(sysUserEntities);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SneakyThrows
|
||||||
|
public Map<String, String> export() {
|
||||||
|
List<SysUserEntity> list = list(Wrappers.lambdaQuery(SysUserEntity.class).eq(SysUserEntity::getSuperAdmin, SuperAdminEnum.NO.getValue()));
|
||||||
|
List<SysUserExcelVO> userExcelVOS = SysUserConvert.INSTANCE.convert2List(list);
|
||||||
|
|
||||||
|
File file = File.createTempFile("system_user_excel", ".xlsx");
|
||||||
|
// 写入到文件
|
||||||
|
ExcelUtils.excelExport(SysUserExcelVO.class, file, userExcelVOS);
|
||||||
|
|
||||||
|
byte[] data = IoUtil.readBytes(Files.newInputStream(file.toPath()));
|
||||||
|
String path = storageService.getPath(file.getName());
|
||||||
|
String url = storageService.upload(data, path);
|
||||||
|
Map<String, String> map = new HashMap<>(2);
|
||||||
|
map.put("path", url);
|
||||||
|
map.put("filename", file.getName());
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,96 @@
|
||||||
|
package net.maku.system.vo;
|
||||||
|
|
||||||
|
import com.alibaba.excel.annotation.ExcelProperty;
|
||||||
|
import com.alibaba.excel.converters.Converter;
|
||||||
|
import com.alibaba.excel.metadata.GlobalConfiguration;
|
||||||
|
import com.alibaba.excel.metadata.data.ReadCellData;
|
||||||
|
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||||
|
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
import net.maku.framework.common.excel.DateConverter;
|
||||||
|
import net.maku.framework.common.utils.DateUtils;
|
||||||
|
import net.maku.system.enums.SuperAdminEnum;
|
||||||
|
import net.maku.system.enums.UserGenderEnum;
|
||||||
|
import net.maku.system.enums.UserStatusEnum;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* excel用户表
|
||||||
|
*
|
||||||
|
* @author 阿沐 babamu@126.com
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class SysUserExcelVO implements Serializable {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@ExcelProperty("用户名")
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@ExcelProperty("姓名")
|
||||||
|
private String realName;
|
||||||
|
|
||||||
|
@ExcelProperty(value = "性别", converter = GenderConverter.class)
|
||||||
|
private Integer gender;
|
||||||
|
|
||||||
|
@ExcelProperty("邮箱")
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
@ExcelProperty("手机号")
|
||||||
|
private String mobile;
|
||||||
|
|
||||||
|
@ExcelProperty("机构ID")
|
||||||
|
private Long orgId;
|
||||||
|
|
||||||
|
@ExcelProperty(value = "状态", converter = StatusConverter.class)
|
||||||
|
private Integer status;
|
||||||
|
|
||||||
|
@ExcelProperty(value = "超级管理员", converter = SuperConverter.class)
|
||||||
|
private Integer superAdmin;
|
||||||
|
|
||||||
|
@ExcelProperty(value = "创建时间", converter = DateConverter.class)
|
||||||
|
private Date createTime;
|
||||||
|
|
||||||
|
public static class GenderConverter implements Converter<Integer> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||||
|
String dateString = cellData.getStringValue();
|
||||||
|
return UserGenderEnum.getValueByName(dateString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WriteCellData<String> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||||
|
return new WriteCellData<>(UserGenderEnum.getNameByValue(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class StatusConverter implements Converter<Integer> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||||
|
String dateString = cellData.getStringValue();
|
||||||
|
return UserStatusEnum.getValueByName(dateString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WriteCellData<String> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||||
|
return new WriteCellData<>(UserStatusEnum.getNameByValue(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class SuperConverter implements Converter<Integer> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||||
|
String dateString = cellData.getStringValue();
|
||||||
|
return SuperAdminEnum.getValueByName(dateString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WriteCellData<String> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||||
|
return new WriteCellData<>(SuperAdminEnum.getNameByValue(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -41,9 +41,10 @@ public class ExcelDataListener<T> extends AnalysisEventListener<T> {
|
||||||
@Override
|
@Override
|
||||||
public void invoke(T data, AnalysisContext context) {
|
public void invoke(T data, AnalysisContext context) {
|
||||||
list.add(data);
|
list.add(data);
|
||||||
if (list.size() == 1) {
|
if (list.size() == 500) {
|
||||||
System.out.println("自己逻辑");
|
System.out.println(("自己逻辑..."));
|
||||||
|
this.callBack.doSaveBatch(list);
|
||||||
|
list.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import com.alibaba.excel.EasyExcel;
|
||||||
import com.alibaba.excel.support.ExcelTypeEnum;
|
import com.alibaba.excel.support.ExcelTypeEnum;
|
||||||
import net.maku.framework.common.excel.ExcelDataListener;
|
import net.maku.framework.common.excel.ExcelDataListener;
|
||||||
import net.maku.framework.common.excel.ExcelFinishCallBack;
|
import net.maku.framework.common.excel.ExcelFinishCallBack;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
@ -18,6 +19,31 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
public class ExcelUtils {
|
public class ExcelUtils {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断excel文件类型
|
||||||
|
*
|
||||||
|
* @param file 源头文件
|
||||||
|
* @return type
|
||||||
|
*/
|
||||||
|
public static ExcelTypeEnum getExcelFileType(MultipartFile file) {
|
||||||
|
String filename = file.getOriginalFilename();
|
||||||
|
if (StringUtils.isNotBlank(filename)) {
|
||||||
|
filename = filename.substring(filename.lastIndexOf("."));
|
||||||
|
switch (filename) {
|
||||||
|
case ".csv":
|
||||||
|
return ExcelTypeEnum.CSV;
|
||||||
|
case ".xls":
|
||||||
|
return ExcelTypeEnum.XLS;
|
||||||
|
case ".xlsx":
|
||||||
|
return ExcelTypeEnum.XLSX;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("无效的文件");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException("无效的文件");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 读取excel文件
|
* 读取excel文件
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import net.maku.framework.common.utils.ExcelUtils;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -21,7 +20,7 @@ import java.util.List;
|
||||||
public class EasyExcelTest {
|
public class EasyExcelTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void doImport() throws FileNotFoundException {
|
public void doImport() {
|
||||||
File file = new File("D://upload//test01.xlsx");
|
File file = new File("D://upload//test01.xlsx");
|
||||||
ExcelClass excelClass = new ExcelClass();
|
ExcelClass excelClass = new ExcelClass();
|
||||||
excelClass.setNumber(1);
|
excelClass.setNumber(1);
|
||||||
|
|
@ -29,12 +28,11 @@ public class EasyExcelTest {
|
||||||
excelClass.setString("test");
|
excelClass.setString("test");
|
||||||
excelClass.setDate(new Date());
|
excelClass.setDate(new Date());
|
||||||
List<ExcelClass> data = Arrays.asList(excelClass, excelClass, excelClass);
|
List<ExcelClass> data = Arrays.asList(excelClass, excelClass, excelClass);
|
||||||
if (!file.exists()) {
|
if (file.exists()) {
|
||||||
ExcelUtils.excelExport(ExcelClass.class, file, data);
|
ExcelUtils.excelExport(ExcelClass.class, file, data);
|
||||||
|
List<ExcelClass> list = ExcelUtils.readSync(file, ExcelClass.class);
|
||||||
|
list.forEach(System.out::println);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<ExcelClass> list = ExcelUtils.readSync(file, ExcelClass.class);
|
|
||||||
list.forEach(System.out::println);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -49,10 +47,10 @@ public class EasyExcelTest {
|
||||||
List<ExcelClass> data = Arrays.asList(excelClass, excelClass, excelClass, excelClass, excelClass, excelClass, excelClass);
|
List<ExcelClass> data = Arrays.asList(excelClass, excelClass, excelClass, excelClass, excelClass, excelClass, excelClass);
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
ExcelUtils.excelExport(ExcelClass.class, file, data);
|
ExcelUtils.excelExport(ExcelClass.class, file, data);
|
||||||
|
ExcelUtils.readAnalysis(file, ExcelClass.class, new ServiceA());
|
||||||
}
|
}
|
||||||
ExcelUtils.readAnalysis(file, ExcelClass.class, new ServiceA());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public static class ExcelClass {
|
public static class ExcelClass {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user