国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

springboot中使用EasyExcel實(shí)現(xiàn)Excel 導(dǎo)入導(dǎo)出

這篇具有很好參考價(jià)值的文章主要介紹了springboot中使用EasyExcel實(shí)現(xiàn)Excel 導(dǎo)入導(dǎo)出。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一、EasyExcel的介紹

EasyExcel 是一款基于 Java 的簡(jiǎn)單易用的 Excel 文件操作工具。它提供了豐富的 API,可以方便地讀取、寫入和操作 Excel 文件,支持常見的 Excel 操作,如讀取/寫入單元格數(shù)據(jù)、合并單元格、設(shè)置樣式、處理大數(shù)據(jù)量等。EasyExcel能大大減少占用內(nèi)存的主要原因是在解析Excel時(shí)沒有將文件數(shù)據(jù)一次性全部加載到內(nèi)存中,而是從磁盤上一行行讀取數(shù)據(jù),逐個(gè)解析。

1、讀取 Excel 文件

使用 EasyExcel 可以讀取 Excel 文件的內(nèi)容并將其轉(zhuǎn)化為 Java 對(duì)象,或者按行進(jìn)行處理。例如:

// 讀取 Excel 文件內(nèi)容并轉(zhuǎn)化為 Java 對(duì)象
List<User> userList = EasyExcel.read("path/to/excel.xlsx").sheet().doReadSync(User.class);

// 按行處理 Excel 文件
EasyExcel.read("path/to/excel.xlsx").sheet().doReadSync(new AnalysisEventListener<User>() {
    @Override
    public void invoke(User user, AnalysisContext context) {
        // 處理每行數(shù)據(jù)
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 處理完成后的回調(diào)
    }
});

2、寫入 Excel 文件

EasyExcel 提供了方便的 API 來寫入數(shù)據(jù)到 Excel 文件中。例如:

// 寫入 Java 對(duì)象到 Excel 文件
EasyExcel.write("path/to/excel.xlsx").sheet().doWrite(userList);

// 寫入指定數(shù)據(jù)到 Excel 文件
List<List<Object>> data = new ArrayList<>();
// 添加數(shù)據(jù)到 data 列表...
EasyExcel.write("path/to/excel.xlsx").sheet().doWrite(data);

3、設(shè)置樣式和操作單元格

EasyExcel 允許你設(shè)置單元格的樣式、合并單元格、設(shè)置表頭等操作。例如:

// 設(shè)置表頭
List<List<String>> head = new ArrayList<>();
// 添加表頭數(shù)據(jù)到 head 列表...
EasyExcel.write("path/to/excel.xlsx").head(head).sheet().doWrite(data);

// 設(shè)置單元格樣式
WriteCellStyle style = new WriteCellStyle();
// 設(shè)置樣式屬性...
WriteSheet sheet = EasyExcel.writerSheet(0).build();
sheet.setCellStyle(style);
EasyExcel.write("path/to/excel.xlsx").sheet(sheet).doWrite(data);

// 合并單元格
WriteSheet sheet = EasyExcel.writerSheet(0).build();
sheet.setAutomaticMergeHead(true); // 自動(dòng)合并表頭
sheet.merge(firstRow, lastRow, firstCol, lastCol); // 合并單元格
EasyExcel.write("path/to/excel.xlsx").sheet(sheet).doWrite(data);

4、大數(shù)據(jù)量處理

EasyExcel 提供了一些特性來處理大數(shù)據(jù)量的 Excel 文件,如使用滑動(dòng)窗口來限制內(nèi)存占用、使用回調(diào)接口來處理每行數(shù)據(jù)等。這些特性可以幫助你高效地處理大型 Excel 文件而不會(huì)導(dǎo)致內(nèi)存溢出。例如:

// 大數(shù)據(jù)量寫入
EasyExcel.write("path/to/excel.xlsx", User.class).sheet().registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).doWrite(userList);


二、完整示例(將用戶表導(dǎo)出導(dǎo)入):

1、在pom.xml引入依賴

    <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>easyexcel</artifactId>
        <version>3.3.2</version>
    </dependency>

2、創(chuàng)建導(dǎo)出實(shí)體類跟表頭

**
 * 用戶 Excel 導(dǎo)出 VO
 */
@Data
public class UserExcelVO {

    @ExcelProperty("用戶編號(hào)")
    private Long id;

    @ExcelProperty("用戶名稱")
    private String username;

    @ExcelProperty("用戶昵稱")
    private String nickname;

    @ExcelProperty("用戶郵箱")
    private String email;

    @ExcelProperty("手機(jī)號(hào)碼")
    private String mobile;

    @ExcelProperty(value = "用戶性別", converter = DictConvert.class)
    @DictFormat(DictTypeConstants.USER_SEX)
    private Integer sex;

    @ExcelProperty(value = "帳號(hào)狀態(tài)", converter = DictConvert.class)
    @DictFormat(DictTypeConstants.COMMON_STATUS)
    private Integer status;

    @ExcelProperty("最后登錄IP")
    private String loginIp;

    @ExcelProperty("最后登錄時(shí)間")
    private LocalDateTime loginDate;

    @ExcelProperty("部門名稱")
    private String deptName;

    @ExcelProperty("部門負(fù)責(zé)人")
    private String deptLeaderNickname;

}

① 每個(gè)字段上的 @ExcelProperty (opens new window)注解,聲明 Excel Head 頭部的名字
② 每個(gè)字段的值,就是它對(duì)應(yīng)的 Excel Row 行的數(shù)據(jù)值
③ @ExcelProperty 注解的 converter 屬性是 DictConvert 轉(zhuǎn)換器,通過它將 status = 1 轉(zhuǎn)換成“開啟”列,status = 0 轉(zhuǎn)換成”禁用”列,注解 @DictFormat (opens new window)為對(duì)應(yīng)的字典數(shù)據(jù)的類型。
更多《EasyExcel 中的注解 》

3、ExcelUtils類 寫入以及讀取

public class ExcelUtils {

    /**
     * 將列表以 Excel 響應(yīng)給前端
     *
     * @param response 響應(yīng)
     * @param filename 文件名
     * @param sheetName Excel sheet 名
     * @param head Excel head 頭
     * @param data 數(shù)據(jù)列表哦
     * @param <T> 泛型,保證 head 和 data 類型的一致性
     * @throws IOException 寫入失敗的情況
     */
    public static <T> void write(HttpServletResponse response, String filename, String sheetName,
                                 Class<T> head, List<T> data) throws IOException {
        // 輸出 Excel
        EasyExcel.write(response.getOutputStream(), head)
                .autoCloseStream(false) // 不要自動(dòng)關(guān)閉,交給 Servlet 自己處理
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 長(zhǎng)度,自動(dòng)適配。最大 255 寬度
                .sheet(sheetName).doWrite(data);
        // 設(shè)置 header 和 contentType。寫在最后的原因是,避免報(bào)錯(cuò)時(shí),響應(yīng) contentType 已經(jīng)被修改了
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
    }
    public static <T> List<T> read(MultipartFile file, Class<T> head) throws IOException {
       return EasyExcel.read(file.getInputStream(), head, null)
                .autoCloseStream(false)  // 不要自動(dòng)關(guān)閉,交給 Servlet 自己處理
                .doReadAllSync();
    }
 }
 

4、Controller類實(shí)現(xiàn)

    @GetMapping("/export")
    @Operation(summary = "導(dǎo)出用戶")
    @OperateLog(type = EXPORT)
    public void exportUserList(@Validated UserExportReqVO reqVO,
                               HttpServletResponse response) throws IOException {
        // 獲得用戶列表
        List<UserDO> users = userService.getUserList(reqVO);
        UserExcelVO excelVO = UserConvert.INSTANCE.convert02(user);
        // 輸出
        ExcelUtils.write(response, "用戶數(shù)據(jù).xls", "用戶列表", UserExcelVO.class, excelVO );
    }
    
    @PostMapping("/import")
    @Operation(summary = "導(dǎo)入用戶")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新,默認(rèn)為 false", example = "true")
    })
    public void importExcel(@RequestParam("file") MultipartFile file,
                                                      @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<UserImportExcelVO> list = ExcelUtils.read(file, UserImportExcelVO.class);
    }

5、前端導(dǎo)入實(shí)現(xiàn)

easyexcel springboot,springboot,excel,spring boot,java文章來源地址http://www.zghlxwxcb.cn/news/detail-654318.html

到了這里,關(guān)于springboot中使用EasyExcel實(shí)現(xiàn)Excel 導(dǎo)入導(dǎo)出的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • SpringBoot整合EasyExcel,Excel導(dǎo)入導(dǎo)出就靠它了

    SpringBoot整合EasyExcel,Excel導(dǎo)入導(dǎo)出就靠它了

    作者主頁 :Designer 小鄭 作者簡(jiǎn)介 :3年JAVA全棧開發(fā)經(jīng)驗(yàn),專注JAVA技術(shù)、系統(tǒng)定制、遠(yuǎn)程指導(dǎo),致力于企業(yè)數(shù)字化轉(zhuǎn)型,CSDN學(xué)院、藍(lán)橋云課認(rèn)證講師。 主打方向 :Vue、SpringBoot、微信小程序 本文講解了如何在SpringBoot項(xiàng)目中整合EasyExcel,實(shí)現(xiàn)Excel快捷導(dǎo)入導(dǎo)出,解析Excel導(dǎo)入導(dǎo)

    2024年02月16日
    瀏覽(23)
  • EasyExcel實(shí)現(xiàn)Excel文件導(dǎo)入導(dǎo)出功能

    EasyExcel實(shí)現(xiàn)Excel文件導(dǎo)入導(dǎo)出功能

    Java領(lǐng)域解析、生成Excel比較有名的框架有Apache poi、jxl等。但他們都存在一個(gè)嚴(yán)重的問題就是非常的耗內(nèi)存。如果你的系統(tǒng)并發(fā)量不大的話可能還行,但是一旦并發(fā)上來后一定會(huì)OOM或者JVM頻繁的full gc。 EasyExcel是阿里巴巴開源的一個(gè)excel處理框架,以使用簡(jiǎn)單、節(jié)省內(nèi)存著稱。

    2024年02月02日
    瀏覽(23)
  • Spring Boot集成EasyExcel實(shí)現(xiàn)excel導(dǎo)入導(dǎo)出操作

    Spring Boot集成EasyExcel實(shí)現(xiàn)excel導(dǎo)入導(dǎo)出操作

    Easy Excel 官網(wǎng) Java解析、生成Excel比較有名的框架有Apache poi、jxl。但他們都存在一個(gè)嚴(yán)重的問題就是非常的耗內(nèi)存,poi有一套SAX模式的API可以一定程度的解決一些內(nèi)存溢出的問題,但POI還是有一些缺陷,比如07版Excel解壓縮以及解壓后存儲(chǔ)都是在內(nèi)存中完成的,內(nèi)存消耗依然很

    2024年02月14日
    瀏覽(19)
  • 【二十四】springboot使用EasyExcel和線程池實(shí)現(xiàn)多線程導(dǎo)入Excel數(shù)據(jù)

    【二十四】springboot使用EasyExcel和線程池實(shí)現(xiàn)多線程導(dǎo)入Excel數(shù)據(jù)

    ??springboot篇章整體欄目:? 【一】springboot整合swagger(超詳細(xì) 【二】springboot整合swagger(自定義)(超詳細(xì)) 【三】springboot整合token(超詳細(xì)) 【四】springboot整合mybatis-plus(超詳細(xì))(上) 【五】springboot整合mybatis-plus(超詳細(xì))(下) 【六】springboot整合自定義全局異常

    2023年04月08日
    瀏覽(23)
  • Spring Boot 集成 EasyExcel 3.x 優(yōu)雅實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出

    Spring Boot 集成 EasyExcel 3.x 優(yōu)雅實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出

    本章節(jié)將介紹 Spring Boot 集成 EasyExcel(優(yōu)雅實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出)。 ?? Spring Boot 2.x 實(shí)踐案例(代碼倉庫) EasyExcel 是一個(gè)基于 Java 的、快速、簡(jiǎn)潔、解決大文件內(nèi)存溢出的 Excel 處理工具。它能讓你在不用考慮性能、內(nèi)存的等因素的情況下,快速完成 Excel 的讀、寫等功能。 Ea

    2024年02月03日
    瀏覽(53)
  • SpringBoot整合easyExcel實(shí)現(xiàn)CSV格式文件的導(dǎo)入導(dǎo)出

    目錄 一:pom依賴 二:檢查CSV內(nèi)容格式的工具類 三:Web端進(jìn)行測(cè)試 四:拓展使用 使用hutool工具類來進(jìn)行導(dǎo)出功能

    2024年02月02日
    瀏覽(23)
  • SpringBoot 集成 EasyExcel 3.x 實(shí)現(xiàn) Excel 導(dǎo)出

    SpringBoot 集成 EasyExcel 3.x 實(shí)現(xiàn) Excel 導(dǎo)出

    目錄 ? EasyExcel官方文檔 EasyExcel是什么? EasyExcel注解 ?springboot集成EasyExcel 簡(jiǎn)單入門導(dǎo)出 : 實(shí)體類 ?自定義轉(zhuǎn)換類 測(cè)試一下 復(fù)雜表頭一對(duì)多導(dǎo)出 :? ?自定義注解 定義實(shí)體類 自定義單元格合并策略 ?測(cè)試一下 ? ? EasyExcel官方文檔 EasyExcel官方文檔 - 基于Java的Excel處理工具

    2024年02月13日
    瀏覽(16)
  • 使用EasyExcel實(shí)現(xiàn)導(dǎo)入和導(dǎo)出

    簡(jiǎn)單實(shí)現(xiàn)導(dǎo)入和導(dǎo)出 POM FileUtil controller Service ServiceImpl ExceptionPersonListener ExceptionPersonImportExcel

    2024年02月13日
    瀏覽(24)
  • SpringBoot整合Easyexcel實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出為Excel表格的功能

    SpringBoot整合Easyexcel實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出為Excel表格的功能

    本文主要介紹基于SpringBoot +MyBatis-Plus+Easyexcel+Vue實(shí)現(xiàn)缺陷跟蹤系統(tǒng)中導(dǎo)出缺陷數(shù)據(jù)的功能,實(shí)現(xiàn)效果如下圖: EasyExcel是一個(gè)基于Java的、快速、簡(jiǎn)潔、解決大文件內(nèi)存溢出的Excel處理工具。他能讓你在不用考慮性能、內(nèi)存的等因素的情況下,快速完成Excel的讀、寫等功能。 本文

    2024年02月14日
    瀏覽(31)
  • spring boot導(dǎo)入導(dǎo)出excel,集成EasyExcel

    spring boot導(dǎo)入導(dǎo)出excel,集成EasyExcel

    一、安裝依賴 二、新建導(dǎo)出工具類 三、新建實(shí)體類 @ExcelProperty: 核心注解,value屬性可用來設(shè)置表頭名稱,converter屬性可以用來設(shè)置類型轉(zhuǎn)換器; @ColumnWidth: 用于設(shè)置表格列的寬度; @DateTimeFormat: 用于設(shè)置日期轉(zhuǎn)換格式; @NumberFormat: 用于設(shè)置數(shù)字轉(zhuǎn)換格式。 四、如果需

    2024年02月06日
    瀏覽(32)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包