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 構建測試實體類
@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. 設置單元格大小
類上添加 如下注解
@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;
}效果如下:

到此這篇關于SpringBoot利用EasyExcel實現(xiàn)導出數(shù)據(jù)的文章就介紹到這了,更多相關SpringBoot EasyExcel導出數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mybatis對象List<String> List<Integer>屬性映射方式
這篇文章主要介紹了mybatis對象List<String> List<Integer>屬性映射方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
使用SpringBoot讀取Windows共享文件的代碼示例
在現(xiàn)代企業(yè)環(huán)境中,文件共享是一個常見的需求,Windows共享文件夾(SMB/CIFS協(xié)議)因其易用性和廣泛的兼容性,成為了許多企業(yè)的首選,在Java應用中,尤其是使用Spring Boot框架時,如何讀取Windows共享文件是一個值得探討的話題,本文介紹了使用SpringBoot讀取Windows共享文件2024-11-11
MyBatis-Plus更新對象時將字段值更新為null的四種常見方法
MyBatis-Plus 是一個 MyBatis 的增強工具,在簡化開發(fā)、提高效率方面表現(xiàn)非常出色,而,在使用 MyBatis-Plus 更新對象時,默認情況下是不會將字段值更新為 null 的,如果你需要將某些字段的值更新為 null,有幾種方法可以實現(xiàn),本文將介紹幾種常見的方法2024-11-11
Java編程實現(xiàn)統(tǒng)計數(shù)組中各元素出現(xiàn)次數(shù)的方法
這篇文章主要介紹了Java編程實現(xiàn)統(tǒng)計數(shù)組中各元素出現(xiàn)次數(shù)的方法,涉及java針對數(shù)組的遍歷、比較、運算等相關操作技巧,需要的朋友可以參考下2017-07-07
SpringBoot利用validation實現(xiàn)數(shù)據(jù)校驗完整指南
Spring Boot 提供的 Validation(基于 JSR 303/380 規(guī)范)讓我們能通過注解的方式優(yōu)雅地完成參數(shù)校驗,極大地提升了開發(fā)效率和代碼可讀性,下面我們就來看看具體實現(xiàn)吧2025-09-09

