網(wǎng)站首頁 編程語言 正文
文章目錄
- POM依賴
- 創(chuàng)建實體類
- 寫入Excel文件
POM依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.0</version>
</dependency>
創(chuàng)建實體類
根據(jù)你想要的Excel文件表頭創(chuàng)建對應(yīng)的實體類:
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class WaiteExcel {
// value 是excel文件的表頭 index 是第幾列
@ExcelProperty(value = "用戶名", index = 0)
private String name;
@ExcelProperty(value = "年齡", index = 1)
private String age;
@ExcelProperty(value = "性別", index = 2)
private String gender;
}
寫入Excel文件
public static void main(String[] args) {
// 寫入文件的路徑
final String excelFilePath = "D:/waiteExcel.xlsx";
try {
// 判斷文件是否存在, 不存在則創(chuàng)建
File file = new File(excelFilePath);
if (!file.exists()) {
file.createNewFile();
}
// 向Excel中寫入數(shù)據(jù)
EasyExcel.write(excelFilePath, WaiteExcel.class)
.sheet("寫入的Sheet") // Sheet 自定義名稱
.doWrite(handleWaiteExcelList()); // 寫入的數(shù)據(jù)(此處寫入的是一個列表)
} catch (IOException e) {
log.error("{}", e);
} catch (Exception e) {
log.error("{}", e);
}
}
在我的電腦D盤中就成功創(chuàng)建了waiteExcel文件
waiteExcel文件中的數(shù)據(jù)也被寫入進(jìn)來了
原文鏈接:https://blog.csdn.net/weixin_43657300/article/details/126155836
相關(guān)推薦
- 2022-07-31 C++?容器?Vector?的使用方法_C 語言
- 2022-07-19 react組件通訊的三種方式props:父組件和子組件互相通訊、兄弟組件通訊
- 2022-10-29 Android 開發(fā) | API 指南- Content Provider 應(yīng)用程序的使用方法
- 2022-06-19 WPF項目在設(shè)計界面調(diào)用后臺代碼_實用技巧
- 2022-12-23 Python?tensorflow與pytorch的浮點運算數(shù)如何計算_python
- 2022-12-27 Python?PyQt5實現(xiàn)拖放效果的原理詳解_python
- 2022-07-02 matplotlib之pyplot模塊添加文本、注解(text和annotate)_python
- 2022-04-14 Python報mongod:?error?while?loading?shared?librarie
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支