springboot整合Excel填充數(shù)據(jù)代碼示例
填充一組數(shù)據(jù)
準備模板
封裝數(shù)據(jù)
import java.util.ArrayList; import java.util.List; /** * 使用實體類封裝填充數(shù)據(jù) * * 實體中成員變量名稱需要和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("學生0" + i); fillData.setAge(10 + i); fillDatas.add(fillData); } return fillDatas; } }
類填充形式
@Test public void test1(){ // 加載模板 String templateFile="src/main/resources/excel/templte/fill_data_template1.xlsx"; // 寫入文件 String targetFileName = "單組數(shù)據(jù)填充.xlsx"; // 準備對象數(shù)據(jù)填充 FillData fillData = new FillData(); fillData.setName("學生1"); fillData.setAge(10); // 生成工作簿對象 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"; // 寫入文件 String targetFileName = "單組數(shù)據(jù)填充.xlsx"; // 生成工作簿對象 ExcelWriterBuilder workBookWriter = EasyExcel.write(targetFileName,FillData.class).withTemplate(templateFile); // 使用Map數(shù)據(jù)填充 HashMap<String, String> mapFillData = new HashMap<>(); mapFillData.put("name", "學生1"); mapFillData.put("age", "11"); // 獲取第一個工作表填充并自動關閉流 workBookWriter.sheet().doFill(mapFillData); }
結果
填充多組數(shù)據(jù)
準備模板
注意模板里面與上面相比是多了.的
封裝數(shù)據(jù)
與上面一樣,省略。
填充
@Test public void test02(){ // 加載模板 String templateFile="src/main/resources/excel/templte/fill_data_template2.xlsx"; // 寫入文件 String targetFileName = "多組數(shù)據(jù)填充.xlsx"; List<FillData> fillDatas = FillData.initFillData(); System.out.println(fillDatas); // 生成工作簿對象 ExcelWriterBuilder workBookWriter = EasyExcel.write(targetFileName,FillData.class).withTemplate(templateFile); // 獲取第一個工作表填充并自動關閉流 workBookWriter.sheet().doFill(fillDatas); }
結果
組合填充
準備模板
即有多組數(shù)據(jù)填充,又有單一數(shù)據(jù)填充 。
封裝數(shù)據(jù)
同上。
填充
@Test public void test03(){ // 加載模板 String templateFile="src/main/resources/excel/templte/fill_data_template3.xlsx"; // 目標文件 String targetFileName = "組合數(shù)據(jù)填充.xlsx"; List<FillData> fillDatas = FillData.initFillData(); // 生成工作簿對象 ExcelWriter excelWriter = EasyExcel.write(targetFileName,FillData.class).withTemplate(templateFile).build(); // 生成工作表對象 WriteSheet writeSheet = EasyExcel.writerSheet().build(); // 組合填充時,因為多組填充的數(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); // 關閉 excelWriter.finish(); }
結果
水平填充
準備模板
水平填充和多組填充模板一樣,不一樣的地方在于,填充時需要通過 FillConfig
對象設置水平填充。
封裝數(shù)據(jù)
同上
填充
@Test public void test04(){ // 加載模板 String templateFile="src/main/resources/excel/templte/fill_data_template4.xlsx"; // 寫入文件 String targetFileName = "水平數(shù)據(jù)填充.xlsx"; List<FillData> fillDatas = FillData.initFillData(); // 生成工作簿對象 ExcelWriter excelWriter = EasyExcel.write(targetFileName,FillData.class).withTemplate(templateFile).build(); // 生成工作表對象 WriteSheet writeSheet = EasyExcel.writerSheet().build(); // 組合填充時,因為多組填充的數(shù)據(jù)量不確定,需要在多組填充完之后另起一行 FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); // 填充 excelWriter.fill(fillDatas, fillConfig, writeSheet); // 關閉 excelWriter.finish(); }
結果
資料
常用類
EasyExcel 入口類,用于構建開始各種操作;
ExcelReaderBuilder 構建出一個ReadWorkbook對象,即一個工作簿對象,對應的是一個Excel文件;
ExcelWriterBuilder 構建出一個WriteWorkbook對象,即一個工作簿對象,對應的是一個Excel文件;
ExcelReaderSheetBuilder 構建出一個ReadSheet對象,即一個工作表的對象,對應的Excel中的每個sheet,一個工作簿可以有多個工作表;
ExcelWriterSheetBuilder 構建出一WriteSheet對象,即一個工作表的對象,對應的Excel中的每個sheet,一個工作簿可以有多個工作表;
ReadListener 在每一行讀取完畢后都會調用ReadListener來處理數(shù)據(jù),我們可以把調用service的代碼可以寫在其invoke方法內部;
WriteHandler 在每一個操作包括創(chuàng)建單元格、創(chuàng)建表格等都會調用WriteHandler來處理數(shù)據(jù),對使用者透明不可見;
所有配置都是繼承的 Workbook的配置會被Sheet繼承。所以在用EasyExcel設置參數(shù)的時候,在EasyExcel…sheet()方法之前作用域是整個sheet,之后針對單個sheet。
讀取時的注解
@ExcelProperty
屬性名 | 含義 | 說明 |
---|---|---|
index | 對應Excel表中的列數(shù) | 默認-1,建議指定時從0開始 |
value | 對應Excel表中的列頭 | |
converter | 成員變量轉換器 | 自定義轉換器需要實Converter接口 |
@ExcelIgnore
標注在成員變量上,默認所有字段都會和excel去匹配,加了這個注解會忽略該字段
@DateTimeFormat
標注在成員變量上,日期轉換,代碼中用 String類型的成員變量
去接收 excel中日期格式的數(shù)據(jù)
會調用這個注解。里面的 value
參照 java.text.SimpleDateFormat
@NumberFormat
標注在成員變量上,數(shù)字轉換,代碼中用 String類型的成員變量
去接收 excel數(shù)字格式的數(shù)據(jù)
會調用這個注解。里面的 value
參照 java.text.DecimalFormat
@ExcelIgnoreUnannotated
標注在類上。不標注該注解時,默認類中所有成員變量都會參與讀寫,無論是否在成員變量上加了@ExcelProperty
的注解。標注該注解后,類中的成員變量如果沒有標注@ExcelProperty
注解將不會參與讀寫。
@ExcelProperty
屬性名 | 含義 | 說明 |
---|---|---|
index | 對應Excel表中的列數(shù) | 默認-1,指定時建議從0開始 |
value | 對應Excel表中的列頭 | |
converter | 成員變量轉換器 | 自定義轉換器需要實Converter接口 |
總結
到此這篇關于springboot整合Excel填充數(shù)據(jù)的文章就介紹到這了,更多相關springboot整合Excel填充數(shù)據(jù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
hibernate 中 fetch=FetchType.LAZY 懶加載失敗處理方法
這篇文章主要介紹了hibernate 中 fetch=FetchType.LAZY 懶加載失敗處理方法,需要的朋友可以參考下2017-09-09Java?Timer與TimerTask類使程序計時執(zhí)行
這篇文章主要介紹了Java定時器中的Timer和TimerTask的原理。Timer主要用于Java線程里指定時間或周期運行任務,它是線程安全的,但不提供實時性(real-time)保證。接下來就跟隨小編一起深入了解Timer和TimerTask吧2022-02-02Spring?Kafka中如何通過參數(shù)配置解決超時問題詳解
這篇文章主要給大家介紹了關于Spring?Kafka中如何通過參數(shù)配置解決超時問題的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-01-01springboot之redis cache TTL選項的使用
這篇文章主要介紹了springboot之redis cache TTL選項的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07