@excel注解_java bean 一对多,多对一 poi导出excel表格

  • 最近造了个poi导出 excel轮子
  • 特点
    • java bean 一对多、多对一关系合并单元行
    • 支持图片导出

Bean 一对多关系合并行

e29a81427bfbb65df6f01871e3da2425.png
  • 代码
public class User {    @Excel(name = "编号")    private String serialNumber;    @Excel(name = "国籍")    private String citizenship;    @Excel(name = "省份")    private String province;    @Excel(name = "城市")    private String city;    @Excel(name = "市区")    private String area;        /**     * 一对多合并注解     */    @ExcelCollection    private List userInfoList;}public class UserInfo {  @Excel(name = "头像")  private String avatar;  @Excel(name = "用户名")  private String name;  @Excel(name = "年龄")  private Integer age;  @Excel(name = "性别")  private Integer sex;}
  • 关键是ExcelCollection注解

Bean 多对一关系合并行

89292afa78de7c71194be82ba776587c.png
  • 代码
public class ManyOneBean {    @ExcelCollection    private List manyOne;}public class ManyOne {  @Excel(name = "产品名称")  private String productName;  @Excel(name = "品牌名称")  private String breanName;  @Excel(name = "金额")  private Integer amount;  @Excel(name = "产品原单价", manyOneMerge = true)  private String originalUnitPrice;  @Excel(name = "优惠单价", manyOneMerge = true)  private String discountUnitPrice;  @Excel(name = "总金额", manyOneMerge = true)  private Integer totalAmount;}
  • 关键是ExcelCollection注解以及Excel注解中的manyOneMerge设置值

最后

  • 轮子初步实现以上功能,仅供自己玩耍。后续继续完善,奥力给
自己封装的excel导出/导入,可以根据注解导出excel.本项目一共有13个类,里面还包含了一个反射工具,一个编码工具,10分值了。下面是测试代码 public class Test { public static void main(String[] arg) throws FileNotFoundException, IOException{ testBean(); testMap(); } public static void testBean() throws FileNotFoundException, IOException{ List l = new ArrayList(); for(int i=0;i<100;i++){ l.add(new MyBean()); } //很轻松,只需要二句话就能导出excel BeanExport be = ExportExcel.BeanExport(MyBean.class); be.createBeanSheet("1月份", "1月份人员信息").addData(l); be.createBeanSheet("2月份","2月份人员信息").addData(l); be.writeFile("E:/test/bean人员信息8.xlsx"); } //如果不想用注解,还能根据MAP导出. public static void testMap () throws FileNotFoundException, IOException{ List l = new ArrayList(); l.add(new MapHeader("姓名","name",5000)); l.add(new MapHeader("年龄","age",4000)); l.add(new MapHeader("生日","birthdate",3000)); l.add(new MapHeader("地址","address",5000)); l.add(new MapHeader("双精度","d",4000)); l.add(new MapHeader("float","f",6000)); List<Map> lm = new ArrayList<Map>(); for(int i=0;i<100;i++){ Map map = new HashMap(); map.put("name","闪电球"); map.put("age",100); map.put("birthdate",new Date()); map.put("address","北京市广东省AAA号123楼!"); map.put("d",22.222d); map.put("f",295.22f); lm.add(map); } MapExport me = ExportExcel.mapExport(l); me.createMapSheel("1月份","广东省人员信息").addData(lm); me.createMapSheel("2月份", "北京市人员信息").addData(lm); me.writeFile("E:/test/map人员信息9.xlsx"); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值