SpringBoot利用EasyExcel實現(xiàn)導出數(shù)據(jù)
1. EasyExcel 介紹
EasyExcel 官網(wǎng)介紹 傳統(tǒng)操作Excel大多都是利用 Apach POI 進行操作的,但是 POI
框架并不完善,使用過程非常繁瑣且有較多的缺陷:
動態(tài)操作Excel非常繁瑣,對于新手來說,很難在短時間內(nèi)上手;
讀寫時需要占用較大的內(nèi)存,當數(shù)據(jù)量大時容易發(fā)生內(nèi)存溢出問題(OOM); 基于上述原因,阿里開源出一款易上手,且比較節(jié)省內(nèi)存的Excel操作框架:EasyExcel
注意:easyExcel底層使用POI實現(xiàn)的;
官網(wǎng)地址:EasyExcel(文檔已經(jīng)遷移) (yuque.com)
2. 導出
2.1 引入依賴
<!--引入easyexcel--> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.0.4</version> </dependency>
2.2 構(gòu)建測試實體類
@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) { //組裝數(shù)據(jù) 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. 設(shè)置單元格大小
類上添加 如下注解
@HeadRowHeight(value = 35) // 表頭行高 @ContentRowHeight(value = 25) // 內(nèi)容行高 @ColumnWidth(value = 50) // 列寬
@Data @NoArgsConstructor @AllArgsConstructor @Builder @HeadRowHeight(value = 35) // 表頭行高 @ContentRowHeight(value = 25) // 內(nèi)容行高 @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; }
效果如下:
到此這篇關(guān)于SpringBoot利用EasyExcel實現(xiàn)導出數(shù)據(jù)的文章就介紹到這了,更多相關(guān)SpringBoot EasyExcel導出數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Apache?Hudi異步Clustering部署操作的掌握
這篇文章主要介紹了Apache?Hudi異步Clustering部署操作的掌握,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-03-03mybatis中如何傳遞單個String類型的參數(shù)
這篇文章主要介紹了mybatis中如何傳遞單個String類型的參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11springboot集成PageHelper分頁失效的原因及解決
項目啟動初期,在集成mybatis的分頁插件,自定義封裝了一個分頁的工具類,方便后期項目的擴展,結(jié)果無法分頁了,怎么設(shè)置搞都沒辦法正常分頁,所以本文將給大家介紹一下springboot集成PageHelper分頁失效的原因及解決,需要的朋友可以參考下2023-10-10聊聊springboot2.2.3升級到2.4.0單元測試的區(qū)別
這篇文章主要介紹了springboot 2.2.3 升級到 2.4.0單元測試的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10spring boot定時任務接收郵件并且存儲附件的方法講解
今天小編就為大家分享一篇關(guān)于spring boot定時任務接收郵件并且存儲附件的方法講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03MyBatis-Plus多表聯(lián)合查詢并且分頁(3表聯(lián)合)
這篇文章主要介紹了MyBatis-Plus多表聯(lián)合查詢并且分頁(3表聯(lián)合),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08Java網(wǎng)絡(luò)編程UDP實現(xiàn)消息發(fā)送及聊天
這篇文章主要為大家詳細介紹了Java網(wǎng)絡(luò)編程UDP實現(xiàn)消息發(fā)送及聊天,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07