1、前端调用封装好的方法$.table.init,传入后台exportUrl

    2、添加导出按钮事件

    1. <a class="btn btn-warning" onclick="$.table.exportExcel()">
    2. <i class="fa fa-download"></i> 导出
    3. </a>
    1. @Excel(name = "用户序号", prompt = "用户编号")
    2. private Long userId;
    3. @Excel(name = "用户名称")
    4. private String userName;
    5. private String sex;
    6. @Excel(name = "最后登陆时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    7. private Date loginDate;

    4、在Controller添加导出方法

    导入实现流程

    1、前端调用封装好的方法$.table.init,传入后台importUrl。

    1. var options = {
    2. importUrl: prefix + "/importData",
    3. columns: [{
    4. field: 'id',
    5. title: '主键'
    6. field: 'name',
    7. title: '名称'
    8. }]
    9. };
    10. $.table.init(options);
    1. <a class="btn btn-info" onclick="$.table.importExcel()">
    2. <i class="fa fa-upload"></i> 导入
    3. </a>

    3、添加导入前端代码,form默认id为importForm,也可指定importExcel(id)

    4、在实体变量上添加@Excel注解,默认为导出导入,也可以单独设置仅导入Type.IMPORT

    @Excel(name = "用户序号")
    private Long id;
    
    @Excel(name = "部门编号", type = Type.IMPORT)
    private Long deptId;
    
    @Excel(name = "用户名称")
    private String userName;
    
    /** 导出部门多个对象 */
    @Excels({
        @Excel(name = "部门名称", targetAttr = "deptName", type = Type.EXPORT),
        @Excel(name = "部门负责人", targetAttr = "leader", type = Type.EXPORT)
    })
    private SysDept dept;
    
    /** 导出部门单个对象 */
    @Excel(name = "部门名称", targetAttr = "deptName", type = Type.EXPORT)
    private SysDept dept;

    5、在Controller添加导入方法,updateSupport属性为是否存在则覆盖(可选)

    @PostMapping("/importData")
    @ResponseBody
    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 = ShiroUtils.getSysUser().getLoginName();
        String message = userService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }