SpringBoot利用EasyExcel實現導出數據
1. EasyExcel 介紹
EasyExcel 官網介紹 傳統(tǒng)操作Excel大多都是利用 Apach POI 進行操作的,但是 POI
框架并不完善,使用過程非常繁瑣且有較多的缺陷:
動態(tài)操作Excel非常繁瑣,對于新手來說,很難在短時間內上手;
讀寫時需要占用較大的內存,當數據量大時容易發(fā)生內存溢出問題(OOM); 基于上述原因,阿里開源出一款易上手,且比較節(jié)省內存的Excel操作框架:EasyExcel
注意:easyExcel底層使用POI實現的;
官網地址:EasyExcel(文檔已經遷移) (yuque.com)
2. 導出
2.1 引入依賴
<!--引入easyexcel--> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.0.4</version> </dependency>
2.2 構建測試實體類
@Data @NoArgsConstructor @AllArgsConstructor @Builder public class User implements Serializable { @ExcelProperty(value = {"用戶名"},index = 0) private String userName; @ExcelProperty(value = {"年齡"},index = 1) private Integer age; @ExcelProperty(value = {"地址"} ,index = 2) private String address; @ExcelProperty(value = {"生日"},index = 3) //注意:日期格式注解由alibaba.excel提供 // @DateTimeFormat("yyyy-MM-dd HH:mm") private Date birthday; }
導出代碼
public static void main(String[] args) { //組裝數據 ArrayList<User> users = new ArrayList<>(); for (int i = 0; i < 10; i++) { User user = new User(); user.setAddress("西安" + i); user.setUserName("張三" + i); user.setBirthday(new Date()); user.setAge(10 + i); users.add(user); } //不做任何注解處理時,表頭名稱與實體類屬性名稱一致 EasyExcel.write("D:\\用戶.xlsx", User.class).sheet("用戶信息").doWrite(users); }
導出效果
3. 設置單元格大小
類上添加 如下注解
@HeadRowHeight(value = 35) // 表頭行高 @ContentRowHeight(value = 25) // 內容行高 @ColumnWidth(value = 50) // 列寬
@Data @NoArgsConstructor @AllArgsConstructor @Builder @HeadRowHeight(value = 35) // 表頭行高 @ContentRowHeight(value = 25) // 內容行高 @ColumnWidth(value = 50) // 列寬 public class User implements Serializable { @ExcelProperty(value = {"用戶名"},index = 0) private String userName; @ExcelProperty(value = {"年齡"},index = 1) private Integer age; @ExcelProperty(value = {"地址"} ,index = 2) private String address; @ExcelProperty(value = {"生日"},index = 3) //注意:日期格式注解由alibaba.excel提供 // @DateTimeFormat("yyyy-MM-dd HH:mm") private Date birthday; }
效果如下:
到此這篇關于SpringBoot利用EasyExcel實現導出數據的文章就介紹到這了,更多相關SpringBoot EasyExcel導出數據內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Apache?Hudi異步Clustering部署操作的掌握
這篇文章主要介紹了Apache?Hudi異步Clustering部署操作的掌握,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-03-03springboot集成PageHelper分頁失效的原因及解決
項目啟動初期,在集成mybatis的分頁插件,自定義封裝了一個分頁的工具類,方便后期項目的擴展,結果無法分頁了,怎么設置搞都沒辦法正常分頁,所以本文將給大家介紹一下springboot集成PageHelper分頁失效的原因及解決,需要的朋友可以參考下2023-10-10聊聊springboot2.2.3升級到2.4.0單元測試的區(qū)別
這篇文章主要介紹了springboot 2.2.3 升級到 2.4.0單元測試的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10spring boot定時任務接收郵件并且存儲附件的方法講解
今天小編就為大家分享一篇關于spring boot定時任務接收郵件并且存儲附件的方法講解,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03