使用easypoi导出excel 格式化日期 一对多集合导出

  先看一下上一篇我们导出的表格样子:

上一篇提到几个优化点:时间显示、枚举类使用、可能一个用户有多笔订单这种需要合并展示的,试了一下,对于枚举的处理可能还是用replace参数最方便,就不再优化了,对于时间类型的@Excel注解有几个格式化日期相关的注解:

导出excel时可以使用exportFormat这个属性设置日期格式,导入的时候可以使用importFormat这个属性,使用format相当于设置了前面两个,例如你可以这样设置:@Excel(name = "生产时间",exportFormat = "yyyy-mm-dd hh:mm:ss")

先不说了,一会贴代码,先看下咱这篇要导出表格的样子吧:

如上:双十一刚过几天,店家统计一下双十一的销售情况,张三买了两个手机,一个华为一个小米,消费能力还是挺强的,李四就比较屌丝了,买了一个360手机,不知道大家用过360手机吗,其实还是挺好用的,我用过一款360N5,挺好的,性价比很高,可惜360不做手机业务了,扯远了,贴下导出实体的代码吧:

@Data
public class UserGoodsDto {
    @Excel(name = "用户名",needMerge = true)
    private String userName;
    @ExcelCollection(name = "商品")
    private List<UserGoods> userGoodsList;
}
@Data
public class UserGoods {
    private Integer id;
    @Excel(name = "商品名")
    private String goodsName;
    @Excel(name = "收货地址")
    private String goodsAddress;
    @Excel(name = "下单时间",exportFormat = "yyyy-MM-dd HH:mm:ss")
    private Date createdTime;
}
简单说明一下:先说下needMerge属性,官方文档这样描述:是否需要纵向合并单元格(用于含有list中,单个的单元格,合并list创建的多个row),因为我们的张三同学买了两件商品,是需要合并的,所以加上needMerge=true即可,

另外时间也使用了上面说的exportFormat属性进行了格式化,使用到的另一个注解就是@ExcelCollection,对于@ExcelCollection官方这样描述:一对多的集合注解,用以标记集合是否被数据以及集合的整体排序。这个解释就比较难受了,我反正是没明白后半句,好在前半句一对多的集合注解好理解,反正就是一对多的时候使用的呗,用户跟商品也是一对多,那就试试呗,@ExcelCollection注解的效果就是如上图,可以实现嵌套集合的展示。

顺便贴一下xml吧:

    <resultMap id="userGoodsMap" type="com.example.springbootmp.dto.user.UserGoodsDto">
        <result property="userName" column="user_name" />
        <collection property="userGoodsList" column="user_id" ofType="com.example.springbootmp.domain.UserGoods">
            <result property="goodsName" column="goods_name" />
            <result property="goodsAddress" column="goods_address" />
            <result property="createdTime" column="created_time" />
        </collection>
    </resultMap>
 
    <select id="selectUserGoods" resultMap="userGoodsMap">
        select tu.user_name,tug.goods_name,tug.goods_address,tug.created_time
        from t_user_excel tu
                 left join t_user_goods_excel tug on tu.id = tug.user_id
    </select>
忘记或者不太清楚mybatis一对多关联查询的小伙伴可以参考下CSDNhttps://mp.csdn.net/editor/html/108537464:springboot整合mybatis使用collection查询 一对多 多对一 多对多查询 

最后贴下controller的导出方法代码,其它的就不贴了,

    @GetMapping("/goods")
    public void export1(HttpServletResponse response) throws IOException {
        List<UserGoodsDto> userGoodsDtos = userExcelService.selectUserGoods();
        System.out.println(userGoodsDtos);
        ExportParams exportParams = new ExportParams("双十一客户下单情况",null);
        ExcelUtil.exportExcel(userGoodsDtos,UserGoodsDto.class,"测试",exportParams,response);
    }

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值