easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 语言(熟悉的表达式语法),完成以前复杂的写法。
1.引入依赖包
<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>3.2.0</version>
</dependency>
2.构建导入导出实体
@Getter
@Setter
@ToString
// 必须是public修饰
public class User {@Excel(name = "姓名")private String userName;@Excel(name = "性别")private String sex;@Excel(name = "年龄")private Integer age;public User(String userName, String sex, Integer age) {this.userName = userName;this.sex = sex;this.age = age;}public User() {}
}
3.导入导出实现方法
导入导入均是按照多个sheet导入导出实现,单个导入导出也可参照使用。
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {// 固定的名称public static final String EXPORT_TITLE = "title";public static final String EXPORT_ENTITY = "entity";public static final String EXPORT_DATA = "data";@PostMapping("/importIndex")public CxResult<Boolean> importIndex(@RequestParam("file") MultipartFile multipartFile) throws Exception {ImportParams params = new ImportParams();params.setHeadRows(1);params.setStartRows(0);params.setStartSheetIndex(0);List<User> userList = ExcelImportUtil.importExcel(multipartFile.getInputStream(), User.class, params);log.info("userList = {}", userList);ImportParams params2 = new ImportParams();params2.setHeadRows(1);params2.setStartRows(0);params2.setStartSheetIndex(1);List<User> userList2 = ExcelImportUtil.importExcel(multipartFile.getInputStream(), User.class, params2);log.info("userList2 = {}", userList2);return CxResult.success(Boolean.TRUE);}@GetMapping("/exportIndex")public void exportIndex(HttpServletResponse response) throws Exception {List<User> userList = Lists.newArrayList();userList.add(new User("张三", "男", 23));userList.add(new User("李四", "男", 24));List<User> userList1 = Lists.newArrayList();userList1.add(new User("丫丫", "女", 23));userList1.add(new User("丫蛋", "女", 24));List<Map<String, Object>> sheetsList = Lists.newArrayList();Map<String, Object> exportMap = Maps.newHashMap();exportMap.put(EXPORT_TITLE, new ExportParams(null, "男", ExcelType.HSSF));exportMap.put(EXPORT_ENTITY, User.class);exportMap.put(EXPORT_DATA, userList);sheetsList.add(exportMap);Map<String, Object> exportMap1 = Maps.newHashMap();exportMap1.put(EXPORT_TITLE, new ExportParams(null, "女", ExcelType.HSSF));exportMap1.put(EXPORT_ENTITY, User.class);exportMap1.put(EXPORT_DATA, userList1);sheetsList.add(exportMap1);Workbook workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);String fileName = DateUtils.dateTimeFormat() + ".xls";response.setContentType("application/octet-stream");response.setHeader("Content-disposition", "attachment;filename=" + fileName);OutputStream outputStream = response.getOutputStream();response.flushBuffer();workbook.write(outputStream);// 写完数据关闭流outputStream.close();}}
4.Easypoi VS Easyexcel
相同点:
easypoi和easyexcel都是基于apache poi进行二次开发的。
差异点:
easypoi 在读写数据的时候,优先是先将数据写入内存,优点是读写性能非常高,但是当数据量很大的时候,会出现oom,当然它也提供了 sax 模式的读写方式,需要调用特定的方法实现。
easyexcel 基于sax模式进行读写数据,不会出现oom情况,程序有过高并发场景的验证,因此程序运行比较稳定,相对于 easypoi 来说,读写性能稍慢!
总结:
easypoi 对定制化的导出支持非常的丰富,如果当前的项目需求,并发量不大、数据量也不大,但是需要导出 excel 的文件样式千差万别,那么我推荐你用 easypoi;反之使用 easyexcel。
教程网站:
EasyPoi教程_V1.0