JAVA基于poi实现操作Excel

一.源码

    1. 码云地址: http://git.oschina.net/Crab2Died/Excel4J

    2. github地址: https://github.com/Crab2died/Excel4J

二.快速实现

    1.对象模型(/src/test/java/moudles/Student.java) 基于ExcelField注解

    // 学号
    @ExcelField(title = "学号", order = 1)
    private String id;

    // 姓名
    @ExcelField(title = "姓名", order = 2)
    private String name;

    // 班级
    @ExcelField(title = "班级", order = 3)
    private String classes;

    2.Excel转对象模型(/src/test/java/base/Excel2Moudle.java)

        2-1. Excel文件对象(截图)

          

        2-2. 转换函数(Excel内容转对象模型)

  @Test
  public void excel2Object(){
      String path = "D:\\IdeaSpace\\Excel4J\\src\\test\\java\\resource\\student.xlsx";
      List<Object> students = ExcelUtil.getInstance().readExcel2ObjsByClasspath(path, Student.class);
      for (Object obj : students){
          Student stu = (Student) obj;
          System.out.println(stu.getName() + " -- " + stu.getClasses());
      }

      students  = ExcelUtil.getInstance().readExcel2ObjsByClasspath(path, Student.class, 0, 2);
      for (Object obj : students){
          Student stu = (Student) obj;
          System.out.println(stu.getName() + " -- " + stu.getClasses());
      }
  }


    3. 对象导出Excel(/src/test/java/base/Moudle2Excel.java)

        3-1. Excel模板(截图)

         

        3-2. 导出函数(对象写入Excel)

 @Test
 public void object2Excel(){
     String tempPath = "D:\\IdeaSpace\\Excel4J\\src\\test\\java\\resource\\template.xlsx";
     List<Student> list = new ArrayList<>();
     list.add(new Student("1010001", "盖伦", "六年级三班"));
     list.add(new Student("1010002", "古尔丹", "一年级三班"));
     list.add(new Student("1010003", "蒙多", "六年级一班"));
     list.add(new Student("1010004", "萝卜特", "三年级二班"));
     Map<String, String> datas = new HashMap<>();
     datas.put("title", "战争学院花名册");
     datas.put("info", "学校统一花名册");
     ExcelUtil.getInstance().exportObj2ExcelByTemplate(datas, tempPath, "D:\\2.xlsx", list, Student.class, false, true);
 }


        3-3. 导出效果图

          

三.其他

        上诉只是最基本功能,全部功能在/src/main/java/com/github/ExcelUtil.java工具类里

        源码地址:

        1. 码云地址: http://git.oschina.net/Crab2Died/Excel4J

        2. github地址: https://github.com/Crab2died/Excel4J

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值