Excel4J基于 poi 的 Excel 操作组件
Excel4J 是基于 poi 的 excel 操作组件,大大减少代码量,提高开发效率。
基于注解
@ExcelField(title = "学号", order = 1) private Long id; @ExcelField(title = "姓名", order = 2) private String name; @ExcelField(title = "入学日期", order = 3) private Date date; @ExcelField(title = "班级", order = 4) private Integer classes; @ExcelField(title = "是否开除", order = 5) private String expel;
读取Excel快速实现
待读取Excel(截图)
转换函数(/src/test/java/base/Excel2Module.java#excel2Object2)
@Test public void excel2Object2() throws Exception { String path = "D:\\IdeaSpace\\Excel4J\\src\\test\\resource\\students_02.xlsx"; // 不基于注解,将Excel内容读至List>对象内 List
> lists = ExcelUtils.getInstance().readExcel2List(path, 1, 3, 0); System.out.println("读取Excel至String数组:"); for (List
list : lists) { System.out.println(list); } // 基于注解,将Excel内容读至List 对象内 List students = ExcelUtils.getInstance().readExcel2Objects(path, Student2.class, 0); System.out.println("读取Excel至对象数组(支持类型转换):"); for (Student2 st : students) { System.out.println(st); } }
转换结果
读取Excel至String数组: [1.0000000000001E13, 张三, 2016/01/19, 101.0, 是] [1.0000000000002E13, 李四, 2017-11-17 10:19:10, 201.0, 否] [1.0000000000004E13, 王二, 2017/11/17, 301.0, 否] 读取Excel至对象数组(支持类型转换): Student2{id=10000000000001, name='张三', date=Tue Jan 19 00:00:00 CST 2016, classes=101, expel='是'} Student2{id=10000000000002, name='李四', date=Fri Nov 17 10:19:10 CST 2017, classes=201, expel='否'} Student2{id=10000000000004, name='王二', date=Fri Nov 17 00:00:00 CST 2017, classes=301, expel='否'}
导出Excel
不基于模板快速导出
导出函数(/src/test/java/base/Module2Excel.java#testList2Excel)
@Test public void testList2Excel() throws Exception { List> list2 = new ArrayList(); List
header = new ArrayList(); for (int i = 0; i _list = new ArrayList(); for (int j = 0; j 导出效果(截图)
基于模板List导出
导出函数(/src/test/java/base/Module2Excel.java#testObject2Excel)
@Test public void testObject2Excel() throws Exception { String tempPath = "D:\\IdeaSpace\\Excel4J\\src\\test\\resource\\normal_template.xlsx"; Listlist = new ArrayList(); list.add(new Student1("1010001", "盖伦", "六年级三班")); list.add(new Student1("1010002", "古尔丹", "一年级三班")); list.add(new Student1("1010003", "蒙多(被开除了)", "六年级一班")); list.add(new Student1("1010004", "萝卜特", "三年级二班")); list.add(new Student1("1010005", "奥拉基", "三年级二班")); list.add(new Student1("1010006", "得嘞", "四年级二班")); list.add(new Student1("1010007", "瓜娃子", "五年级一班")); list.add(new Student1("1010008", "战三", "二年级一班")); list.add(new Student1("1010009", "李四", "一年级一班")); Map data = new HashMap(); data.put("title", "战争学院花名册"); data.put("info", "学校统一花名册"); // 基于模板导出Excel ExcelUtils.getInstance().exportObjects2Excel(tempPath, 0, list, data, Student1.class, false, "D:/A.xlsx"); // 不基于模板导出Excel ExcelUtils.getInstance().exportObjects2Excel(list, Student1.class, true, null, true, "D:/B.xlsx"); } 导出模板(截图)
基于模板导出结果(截图)
不基于模板导出结果(截图)
基于模板Map>导出
导出函数(/src/test/java/base/Module2Excel.java#testMap2Excel)
@Test public void testMap2Excel() throws Exception { Mapclasses = new HashMap(); Map data = new HashMap(); data.put("title", "战争学院花名册"); data.put("info", "学校统一花名册"); classes.put("class_one", new ArrayList () {{ add(new Student1("1010009", "李四", "一年级一班")); add(new Student1("1010002", "古尔丹", "一年级三班")); }}); classes.put("class_two", new ArrayList () {{ add(new Student1("1010008", "战三", "二年级一班")); }}); classes.put("class_three", new ArrayList () {{ add(new Student1("1010004", "萝卜特", "三年级二班")); add(new Student1("1010005", "奥拉基", "三年级二班")); }}); classes.put("class_four", new ArrayList () {{ add(new Student1("1010006", "得嘞", "四年级二班")); }}); classes.put("class_six", new ArrayList () {{ add(new Student1("1010001", "盖伦", "六年级三班")); add(new Student1("1010003", "蒙多", "六年级一班")); }}); ExcelUtils.getInstance().exportObject2Excel("D:\\IdeaSpace\\Excel4J\\src\\test\\resource\\map_template.xlsx", 0, classes, data, Student1.class, false, "D:/C.xlsx"); } 导出模板(截图)
导出结果(截图)
评论