导入导出EasyPOI @excel注解属性作用

目录

1、@Excel注解使用:

 2、@ExcelTarget注解使用:

 3、@ExcelEntity注解使用:

 4、 @ExcelCollection注解使用:

 5、示例:

1、@Excel注解使用:

@Excel 作用到filed上面,是对Excel一列的一个描述,这个是必须使用的注解,如果需求简单只使用这一个注解也是可以的,涵盖了常用的Excel需求。

 2、@ExcelTarget注解使用:

这个是作用在导出的实体类上面,属性也只有一个id。

 3、@ExcelEntity注解使用:

@ExcelEntity标记是不是导出excel 标记为实体类,一遍是一个内部属性类,标记是否继续穿透,可以自定义内部id。

4、 @ExcelCollection注解使用:

@ExcelCollection一对多的集合注解,用以标记集合是否被数据以及集合的整体排序。

 5、示例:

1、实体类:

@Data
@ApiModel("船员信息对象")
@JsonIgnoreProperties(value = {"handler"})//是json序列化时忽略bean中的一些属性序列化和反序列化时抛出的异常.
public class DmCrewInfo  {
   
    /**
     * id
     */
    @Id
    @ApiModelProperty(value = "id")
    private String id;
    /**
     * 船舶
     */
    @ApiModelProperty(value = "船舶")
    @Excel(name = "船舶", orderNum = "1")
    private String shipName;
    /**
     * 姓名
     */
    @ApiModelProperty(value = "姓名")
    @Excel(name = "姓名", orderNum = "2")
    private String name;
    /**
     * 出生年月
     */
    @ApiModelProperty(value = "出生年月")
    @Excel(name = "出生年月", format = "yyyy-MM", orderNum = "3")
    private Date birthday;
    /**
     * 手机号码
     */
    @ApiModelProperty(value = "手机号码")
    @Excel(name = "手机号码", orderNum = "4", width = 20)
    private String phone;
    /**
     * 年龄
     */
    @ApiModelProperty(value = "年龄")
    @Excel(name = "年龄", orderNum = "5")
    private Long age;

}

2、调用导出方法:

 //allList为需要导出的实体类集合
List<DmCrewInfo> allList = dmCrewInfoMapper.selectDmCrewInfoList(exportDmCrewInfo);
 if (allList != null && allList.size() > 0) {
            // 导出数据
            ExcelUtil.exportExcel(allList, "船员信息数据", "船员信息数据",
                    DmCrewInfo.class, "船员信息数据导出.xls", response);
        }

3、效果:

 

  • 9
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Easypoi 是一个基于 Apache POI 和 Jxls 封装的 Java Excel 工具类库,提供了丰富的导入导出功能。以下是 Easypoi导入导出功能的使用方法: 1. 导出 Excel (1)创建实体类,实体类中的属性需要添加 @Excel 注解,如下所示: ``` public class User { @Excel(name = "ID", orderNum = "0") private Integer id; @Excel(name = "姓名", orderNum = "1") private String name; @Excel(name = "年龄", orderNum = "2") private Integer age; } ``` (2)调用 ExcelExportUtil.exportExcel 方法导出 Excel,示例代码如下: ``` List<User> userList = userService.getUserList(); Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户列表", "用户"), User.class, userList); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("用户列表.xls", "UTF-8")); workbook.write(response.getOutputStream()); ``` 2. 导入 Excel (1)创建实体类,实体类中的属性需要添加 @Excel 注解,如下所示: ``` public class User { @Excel(name = "ID", orderNum = "0") private Integer id; @Excel(name = "姓名", orderNum = "1") private String name; @Excel(name = "年龄", orderNum = "2") private Integer age; } ``` (2)调用 ExcelImportUtil.importExcel 方法导入 Excel,示例代码如下: ``` List<User> userList = ExcelImportUtil.importExcel(new File("用户列表.xls"), User.class, new ImportParams()); userService.saveUserList(userList); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值