依赖:
<dependencies> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>4.1.0</version> </dependency> </dependencies>
采用注解导出导入
easypoi 最大的亮点就是基于注解实体类来导出、导入excel,使用起来非常简单!
@Data public class UserEntity { @Excel(name = "姓名") private String name; @Excel(name = "年龄") private int age; @Excel(name = "操作时间", format = "yyyy-MM-dd HH:mm:ss", width = 20.0) private Date time; }
导出
public class Export { public static void main(String[] args) throws Exception { List<UserEntity> dataList = new ArrayList<>(); for (int i = 0; i < 10; i++) { UserEntity userEntity = new UserEntity(); userEntity.setName("张三" + i); userEntity.setAge(20 + i); userEntity.setTime(new Date(System.currentTimeMillis() + i)); dataList.add(userEntity); } //生成excel文档 Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户", "用户信息"), UserEntity.class, dataList); FileOutputStream fos = new FileOutputStream("execl/easypoi-user1.xls"); workbook.write(fos); fos.close(); } }
导入
public class Import { public static void main(String[] args) { ImportParams params = new ImportParams(); params.setTitleRows(1); params.setHeadRows(1); long start = new Date().getTime(); List<UserEntity> list = ExcelImportUtil.importExcel(new File("execl/easypoi-user1.xls"), UserEntity.class, params); System.out.println(new Date().getTime() - start); System.out.println(JSONArray.toJSONString(list)); } }
自定义数据导出 excel
public class CustomExport { public static void main(String[] args) throws Exception { //封装表头 List<ExcelExportEntity> entityList = new ArrayList<ExcelExportEntity>(); entityList.add(new ExcelExportEntity("姓名", "name")); entityList.add(new ExcelExportEntity("年龄", "age")); ExcelExportEntity entityTime = new ExcelExportEntity("操作时间", "time"); entityTime.setFormat("yyyy-MM-dd HH:mm:ss"); entityTime.setWidth(20.0); entityList.add(entityTime); //封装数据体 List<Map<String, Object>> dataList = new ArrayList<>(); for (int i = 0; i < 10; i++) { Map<String, Object> userEntityMap = new HashMap<>(); userEntityMap.put("name", "张三" + i); userEntityMap.put("age", 20 + i); userEntityMap.put("time", new Date(System.currentTimeMillis() + i)); dataList.add(userEntityMap); } //生成excel文档 Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("学生", "用户信息"), entityList, dataList); FileOutputStream fos = new FileOutputStream("execl/easypoi-user2.xls"); workbook.write(fos); fos.close(); } }
自定义导入 excel
public class CustomImport { public static void main(String[] args) { ImportParams params = new ImportParams(); params.setTitleRows(1); params.setHeadRows(1); long start = new Date().getTime(); List<Map<String, Object>> list = ExcelImportUtil.importExcel(new File("execl/easypoi-user2.xls"), Map.class, params); System.out.println(new Date().getTime() - start); System.out.println(JSONArray.toJSONString(list)); } }