JAVA基于poi实现操作Excel

原创
2017/05/29 14:29
阅读数 3.7K

一.源码

    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

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部