官网:EasyExcel
其他简单Excel框架:ExcelUtil
1、pom依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.6</version> </dependency>
2、实体类
@Data @NoArgsConstructor @AllArgsConstructor public class User { @ExcelProperty(value = "姓名") private String userName; @NumberFormat("#.##%") @ExcelProperty(value = "体脂率") private double fat; @ExcelProperty(value = "编写日期") private String birthday; }
3、导出数据
@GetMapping(value = "/write") public String write(){ String url = "/Users/mac/Desktop/76521.xlsx"; List<User> list = Arrays.asList( new User("刘飞",0.2512,"2002-01-12"), new User("张羽",0.2345,"2002-06-22"), new User("关备",0.1812,"2002-04-03") ); EasyExcel.write(url,User.class).sheet("Sheet").doWrite(list); return "OK"; }
测试如下:
4、导入
public class DataListener extends AnalysisEventListener<User> { List<User> list = new ArrayList<>(); /** * 这个每一条数据解析都会来调用 * @param user excel对应一个对象 * @param analysisContext 上下文 */ @Override public void invoke(User user, AnalysisContext analysisContext) { list.add(user); } /** * 所有数据解析完成了 最终会调用 * @param analysisContext 上下文 */ @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { System.out.println("所有数据加载完毕,在里面将数据存储到数据库"); System.out.println(JSON.toJSON(list)); } }
接口:
@GetMapping(value = "/read") public String read(){ String url = "/Users/mac/Desktop/76521.xlsx"; EasyExcel.read(url,User.class, new DataListener()).sheet().doRead(); return "OK"; }
测试结果:
测试OK。
来源:oschina
链接:https://my.oschina.net/mdxlcj/blog/4357340