日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

EasyExcel 3.X 簡單寫入Excel文件數(shù)據(jù)

作者:杜小舟 更新時間: 2022-08-05 編程語言

文章目錄

      • 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)來了
在這里插入圖片描述





End


原文鏈接:https://blog.csdn.net/weixin_43657300/article/details/126155836

欄目分類
最近更新