springboot整合Excel填充數(shù)據(jù)代碼示例
填充一組數(shù)據(jù)
準(zhǔn)備模板

封裝數(shù)據(jù)
import java.util.ArrayList;
import java.util.List;
/**
* 使用實(shí)體類(lèi)封裝填充數(shù)據(jù)
*
* 實(shí)體中成員變量名稱(chēng)需要和Excel表各種{}包裹的變量名匹配
*/
@Data
public class FillData {
private String name;
private int age;
// 生成多組數(shù)據(jù)代碼
public static List<FillData> initFillData() {
ArrayList<FillData> fillDatas = new ArrayList<FillData>();
for (int i = 0; i < 10; i++) {
FillData fillData = new FillData();
fillData.setName("學(xué)生0" + i);
fillData.setAge(10 + i);
fillDatas.add(fillData);
}
return fillDatas;
}
}類(lèi)填充形式
@Test
public void test1(){
// 加載模板
String templateFile="src/main/resources/excel/templte/fill_data_template1.xlsx";
// 寫(xiě)入文件
String targetFileName = "單組數(shù)據(jù)填充.xlsx";
// 準(zhǔn)備對(duì)象數(shù)據(jù)填充
FillData fillData = new FillData();
fillData.setName("學(xué)生1");
fillData.setAge(10);
// 生成工作簿對(duì)象
ExcelWriterBuilder workBookWriter = EasyExcel.write(targetFileName,FillData.class).withTemplate(templateFile);
// 獲取工作表并填充
workBookWriter.sheet().doFill(fillData);
}Map形式填充
@Test
public void test1(){
// 加載模板
String templateFile="src/main/resources/excel/templte/fill_data_template1.xlsx";
// 寫(xiě)入文件
String targetFileName = "單組數(shù)據(jù)填充.xlsx";
// 生成工作簿對(duì)象
ExcelWriterBuilder workBookWriter = EasyExcel.write(targetFileName,FillData.class).withTemplate(templateFile);
// 使用Map數(shù)據(jù)填充
HashMap<String, String> mapFillData = new HashMap<>();
mapFillData.put("name", "學(xué)生1");
mapFillData.put("age", "11");
// 獲取第一個(gè)工作表填充并自動(dòng)關(guān)閉流
workBookWriter.sheet().doFill(mapFillData);
}結(jié)果

填充多組數(shù)據(jù)
準(zhǔn)備模板
注意模板里面與上面相比是多了.的

封裝數(shù)據(jù)
與上面一樣,省略。
填充
@Test
public void test02(){
// 加載模板
String templateFile="src/main/resources/excel/templte/fill_data_template2.xlsx";
// 寫(xiě)入文件
String targetFileName = "多組數(shù)據(jù)填充.xlsx";
List<FillData> fillDatas = FillData.initFillData();
System.out.println(fillDatas);
// 生成工作簿對(duì)象
ExcelWriterBuilder workBookWriter = EasyExcel.write(targetFileName,FillData.class).withTemplate(templateFile);
// 獲取第一個(gè)工作表填充并自動(dòng)關(guān)閉流
workBookWriter.sheet().doFill(fillDatas);
}結(jié)果

組合填充
準(zhǔn)備模板
即有多組數(shù)據(jù)填充,又有單一數(shù)據(jù)填充 。

封裝數(shù)據(jù)
同上。
填充
@Test
public void test03(){
// 加載模板
String templateFile="src/main/resources/excel/templte/fill_data_template3.xlsx";
// 目標(biāo)文件
String targetFileName = "組合數(shù)據(jù)填充.xlsx";
List<FillData> fillDatas = FillData.initFillData();
// 生成工作簿對(duì)象
ExcelWriter excelWriter = EasyExcel.write(targetFileName,FillData.class).withTemplate(templateFile).build();
// 生成工作表對(duì)象
WriteSheet writeSheet = EasyExcel.writerSheet().build();
// 組合填充時(shí),因?yàn)槎嘟M填充的數(shù)據(jù)量不確定,需要在多組填充完之后另起一行
FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();
// 填充并換行
excelWriter.fill(fillDatas, fillConfig, writeSheet);
HashMap<String, String> otherData = new HashMap<>();
otherData.put("date", "2020-03-14");
otherData.put("total", "100");
excelWriter.fill(otherData, writeSheet);
// 關(guān)閉
excelWriter.finish();
}結(jié)果

水平填充
準(zhǔn)備模板
水平填充和多組填充模板一樣,不一樣的地方在于,填充時(shí)需要通過(guò) FillConfig 對(duì)象設(shè)置水平填充。

封裝數(shù)據(jù)
同上
填充
@Test
public void test04(){
// 加載模板
String templateFile="src/main/resources/excel/templte/fill_data_template4.xlsx";
// 寫(xiě)入文件
String targetFileName = "水平數(shù)據(jù)填充.xlsx";
List<FillData> fillDatas = FillData.initFillData();
// 生成工作簿對(duì)象
ExcelWriter excelWriter = EasyExcel.write(targetFileName,FillData.class).withTemplate(templateFile).build();
// 生成工作表對(duì)象
WriteSheet writeSheet = EasyExcel.writerSheet().build();
// 組合填充時(shí),因?yàn)槎嘟M填充的數(shù)據(jù)量不確定,需要在多組填充完之后另起一行
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
// 填充
excelWriter.fill(fillDatas, fillConfig, writeSheet);
// 關(guān)閉
excelWriter.finish();
}結(jié)果

資料
常用類(lèi)
EasyExcel 入口類(lèi),用于構(gòu)建開(kāi)始各種操作;
ExcelReaderBuilder 構(gòu)建出一個(gè)ReadWorkbook對(duì)象,即一個(gè)工作簿對(duì)象,對(duì)應(yīng)的是一個(gè)Excel文件;
ExcelWriterBuilder 構(gòu)建出一個(gè)WriteWorkbook對(duì)象,即一個(gè)工作簿對(duì)象,對(duì)應(yīng)的是一個(gè)Excel文件;
ExcelReaderSheetBuilder 構(gòu)建出一個(gè)ReadSheet對(duì)象,即一個(gè)工作表的對(duì)象,對(duì)應(yīng)的Excel中的每個(gè)sheet,一個(gè)工作簿可以有多個(gè)工作表;
ExcelWriterSheetBuilder 構(gòu)建出一WriteSheet對(duì)象,即一個(gè)工作表的對(duì)象,對(duì)應(yīng)的Excel中的每個(gè)sheet,一個(gè)工作簿可以有多個(gè)工作表;
ReadListener 在每一行讀取完畢后都會(huì)調(diào)用ReadListener來(lái)處理數(shù)據(jù),我們可以把調(diào)用service的代碼可以寫(xiě)在其invoke方法內(nèi)部;
WriteHandler 在每一個(gè)操作包括創(chuàng)建單元格、創(chuàng)建表格等都會(huì)調(diào)用WriteHandler來(lái)處理數(shù)據(jù),對(duì)使用者透明不可見(jiàn);
所有配置都是繼承的 Workbook的配置會(huì)被Sheet繼承。所以在用EasyExcel設(shè)置參數(shù)的時(shí)候,在EasyExcel…sheet()方法之前作用域是整個(gè)sheet,之后針對(duì)單個(gè)sheet。
讀取時(shí)的注解
@ExcelProperty
| 屬性名 | 含義 | 說(shuō)明 |
|---|---|---|
| index | 對(duì)應(yīng)Excel表中的列數(shù) | 默認(rèn)-1,建議指定時(shí)從0開(kāi)始 |
| value | 對(duì)應(yīng)Excel表中的列頭 | |
| converter | 成員變量轉(zhuǎn)換器 | 自定義轉(zhuǎn)換器需要實(shí)Converter接口 |
@ExcelIgnore
標(biāo)注在成員變量上,默認(rèn)所有字段都會(huì)和excel去匹配,加了這個(gè)注解會(huì)忽略該字段
@DateTimeFormat
標(biāo)注在成員變量上,日期轉(zhuǎn)換,代碼中用 String類(lèi)型的成員變量 去接收 excel中日期格式的數(shù)據(jù) 會(huì)調(diào)用這個(gè)注解。里面的 value 參照 java.text.SimpleDateFormat
@NumberFormat
標(biāo)注在成員變量上,數(shù)字轉(zhuǎn)換,代碼中用 String類(lèi)型的成員變量 去接收 excel數(shù)字格式的數(shù)據(jù) 會(huì)調(diào)用這個(gè)注解。里面的 value 參照 java.text.DecimalFormat
@ExcelIgnoreUnannotated
標(biāo)注在類(lèi)上。不標(biāo)注該注解時(shí),默認(rèn)類(lèi)中所有成員變量都會(huì)參與讀寫(xiě),無(wú)論是否在成員變量上加了@ExcelProperty 的注解。標(biāo)注該注解后,類(lèi)中的成員變量如果沒(méi)有標(biāo)注@ExcelProperty 注解將不會(huì)參與讀寫(xiě)。
@ExcelProperty
| 屬性名 | 含義 | 說(shuō)明 |
|---|---|---|
| index | 對(duì)應(yīng)Excel表中的列數(shù) | 默認(rèn)-1,指定時(shí)建議從0開(kāi)始 |
| value | 對(duì)應(yīng)Excel表中的列頭 | |
| converter | 成員變量轉(zhuǎn)換器 | 自定義轉(zhuǎn)換器需要實(shí)Converter接口 |
總結(jié)
到此這篇關(guān)于springboot整合Excel填充數(shù)據(jù)的文章就介紹到這了,更多相關(guān)springboot整合Excel填充數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?axios與spring前后端分離傳參規(guī)范總結(jié)
這篇文章主要介紹了Java?axios與spring前后端分離傳參規(guī)范總結(jié),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
Spring?Boot/Spring?MVC核心注解的作用詳解
本文詳細(xì)介紹了SpringBoot和SpringMVC中最常用的15個(gè)核心注解,涵蓋了請(qǐng)求路由映射、參數(shù)綁定、RESTful?API、組件管理、數(shù)據(jù)校驗(yàn)等內(nèi)容,通過(guò)豐富的代碼示例和圖表幫助開(kāi)發(fā)者全面掌握這些注解,提升開(kāi)發(fā)效率和代碼質(zhì)量,感興趣的朋友跟隨小編一起看看吧2025-11-11
util.Date與sql.Date的相互轉(zhuǎn)換過(guò)程
本文介紹了Java中兩個(gè)常用日期類(lèi)的區(qū)別和相互轉(zhuǎn)換方法,包括java.util.Date和java.sql.Date,并展示了如何使用SimpleDateFormat進(jìn)行日期格式化2026-01-01
Java語(yǔ)言實(shí)現(xiàn)非遞歸實(shí)現(xiàn)樹(shù)的前中后序遍歷總結(jié)
今天小編就為大家分享一篇關(guān)于Java語(yǔ)言實(shí)現(xiàn)非遞歸實(shí)現(xiàn)樹(shù)的前中后序遍歷總結(jié),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
SpringBoot使用Kaptcha實(shí)現(xiàn)驗(yàn)證碼的生成與驗(yàn)證功能
這篇文章主要介紹了SpringBoot使用Kaptcha實(shí)現(xiàn)驗(yàn)證碼的生成與驗(yàn)證功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
netty?pipeline中的inbound和outbound事件傳播分析
這篇文章主要為大家介紹了netty?pipeline中的inbound和outbound事件傳播分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

