欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot整合Excel填充數(shù)據(jù)代碼示例

 更新時(shí)間:2023年08月31日 08:44:34   作者:無(wú)語(yǔ)堵上西樓  
這篇文章主要給大家介紹了關(guān)于springboot整合Excel填充數(shù)據(jù)的相關(guān)資料,文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用springboot具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

填充一組數(shù)據(jù)

準(zhǔn)備模板

封裝數(shù)據(jù)

import java.util.ArrayList;
import java.util.List;
/**
 * 使用實(shí)體類封裝填充數(shù)據(jù)
 *
 *  實(shí)體中成員變量名稱需要和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;
        }
}

類填充形式

    @Test
    public void test1(){
        // 加載模板
        String templateFile="src/main/resources/excel/templte/fill_data_template1.xlsx";
        // 寫入文件
        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";
        // 寫入文件
        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";
        // 寫入文件
        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";
        // 寫入文件
        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é)果

資料

常用類

EasyExcel 入口類,用于構(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的代碼可以寫在其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類型的成員變量 去接收 excel中日期格式的數(shù)據(jù) 會(huì)調(diào)用這個(gè)注解。里面的 value 參照 java.text.SimpleDateFormat

@NumberFormat

標(biāo)注在成員變量上,數(shù)字轉(zhuǎn)換,代碼中用 String類型的成員變量 去接收 excel數(shù)字格式的數(shù)據(jù) 會(huì)調(diào)用這個(gè)注解。里面的 value 參照 java.text.DecimalFormat

@ExcelIgnoreUnannotated

標(biāo)注在類上。不標(biāo)注該注解時(shí),默認(rèn)類中所有成員變量都會(huì)參與讀寫,無(wú)論是否在成員變量上加了@ExcelProperty 的注解。標(biāo)注該注解后,類中的成員變量如果沒(méi)有標(biāo)注@ExcelProperty 注解將不會(huì)參與讀寫。

@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)文章

最新評(píng)論