Controller
@RestController @RequestMapping("/system/user") public class SysUserController extends BaseController { @Log(title = "用户管理", businessType = BusinessType.EXPORT) @PreAuthorize("@ss.hasPermi('system:user:export')") @PostMapping("/export") public void export(HttpServletResponse response, SysUser user) { List<SysUser> list = userService.selectUserList(user); ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class); util.exportExcel(response, list, "用户数据"); } @Log(title = "用户管理", businessType = BusinessType.IMPORT) @PreAuthorize("@ss.hasPermi('system:user:import')") @PostMapping("/importData") public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception { ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class); List<SysUser> userList = util.importExcel(file.getInputStream()); String operName = getUsername(); String message = userService.importUser(userList, updateSupport, operName); return AjaxResult.success(message); } @PostMapping("/importTemplate") public void importTemplate(HttpServletResponse response) { ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class); util.importTemplateExcel(response, "用户数据"); } }
Service
public interface ISysUserService extends IService<SysUser> { /** * 导入用户数据 * * @param userList 用户数据列表 * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据 * @param operName 操作用户 * @return 结果 */ String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName); }
Impl
@Service public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService { /** * 导入用户数据 * * @param userList 用户数据列表 * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据 * @param operName 操作用户 * @return 结果 */ @Override public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName) { if (StringUtils.isNull(userList) || userList.size() == 0) { throw new ServiceException("导入用户数据不能为空!"); } int successNum = 0; int failureNum = 0; StringBuilder successMsg = new StringBuilder(); StringBuilder failureMsg = new StringBuilder(); String password = configService.selectConfigByKey("sys.user.initPassword"); for (SysUser user : userList) { try { // 验证是否存在这个用户 SysUser u = baseMapper.selectUserByUserName(user.getUserName()); if (StringUtils.isNull(u)) { BeanValidators.validateWithException(validator, user); user.setPassword(SecurityUtils.encryptPassword(password)); user.setCreateBy(operName); this.insertUser(user); successNum++; successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功"); } else if (isUpdateSupport) { BeanValidators.validateWithException(validator, user); user.setUpdateBy(operName); this.updateUser(user); successNum++; successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功"); } else { failureNum++; failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在"); } } catch (Exception e) { failureNum++; String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败:"; failureMsg.append(msg + e.getMessage()); log.error(msg, e); } } if (failureNum > 0) { failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:"); throw new ServiceException(failureMsg.toString()); } else { successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:"); } return successMsg.toString(); } }
部分源码
导入导出方法
public class ExcelUtil<T> { /** * 实体对象 */ public Class<T> clazz; //有参构造 public ExcelUtil(Class<T> clazz) { this.clazz = clazz; } /** * 对list数据源将其里面的数据导入到excel表单 * * @param response 返回数据 * @param list 导出数据集合 * @param sheetName 工作表的名称 * @return 结果 * @throws IOException */ public void exportExcel(HttpServletResponse response, List<T> list, String sheetName) { exportExcel(response, list, sheetName, StringUtils.EMPTY); } /** * 对excel表单默认第一个索引名转换成list * * @param is 输入流 * @return 转换后集合 */ public List<T> importExcel(InputStream is) throws Exception { return importExcel(is, 0); } /** * 对list数据源将其里面的数据导入到excel表单 * * @param sheetName 工作表的名称 * @return 结果 */ public void importTemplateExcel(HttpServletResponse response, String sheetName) { importTemplateExcel(response, sheetName, StringUtils.EMPTY); } }
HttpServletResponse
HttpServletResponse详解