網(wǎng)站首頁 編程語言 正文
文章目錄
- POM依賴
- 創(chuàng)建實(shí)體類
- 寫入Excel文件
POM依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.0</version>
</dependency>
創(chuàng)建實(shí)體類
根據(jù)你想要的Excel文件表頭創(chuàng)建對應(yīng)的實(shí)體類:
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)推薦
- 2023-05-12 Python時間戳轉(zhuǎn)換為字符串與字符串轉(zhuǎn)換為時間戳_python
- 2022-10-13 Python?數(shù)據(jù)分析教程探索性數(shù)據(jù)分析_python
- 2022-06-29 python人工智能tensorflow構(gòu)建卷積神經(jīng)網(wǎng)絡(luò)CNN_python
- 2022-07-07 在Kubernetes集群中搭建Istio微服務(wù)網(wǎng)格的過程詳解_云其它
- 2022-10-02 SQL堆疊注入簡介_MsSql
- 2022-08-19 Python包中__init__.py文件的作用與用法實(shí)例詳解_python
- 2023-04-04 Golang利用casbin實(shí)現(xiàn)權(quán)限驗(yàn)證詳解_Golang
- 2022-11-23 iOS開發(fā)學(xué)習(xí)?ViewController使用示例詳解_IOS
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 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)雅實(shí)現(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)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支