最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

EasyExcel讀寫(xiě)、模板填充、Web上傳下載入門(mén)到實(shí)戰(zhàn)全指南

 更新時(shí)間:2026年01月26日 08:24:04   作者:五阿哥永琪  
EasyExcel類(lèi)是一套基于Java的開(kāi)源Excel解析工具類(lèi),相較于傳統(tǒng)的框架如Apache poi、jxl等更加快速、簡(jiǎn)潔,還可以解決大文件內(nèi)存溢出問(wèn)題,這篇文章主要介紹了EasyExcel讀寫(xiě)、模板填充、Web上傳下載入門(mén)到實(shí)戰(zhàn)的相關(guān)資料,需要的朋友可以參考下

EasyExcel 是阿里巴巴開(kāi)源的一款 基于 Java 的高性能、簡(jiǎn)單易用的 Excel 讀寫(xiě)工具庫(kù),專(zhuān)注于解決使用 Apache POI(Java 操作 Excel 的底層庫(kù))時(shí)常見(jiàn)的 內(nèi)存溢出(OOM)API 復(fù)雜 問(wèn)題。

一、 主要功能

1. 讀 Excel(支持 .xls / .xlsx)

  • 按行讀取,自動(dòng)映射到 Java 對(duì)象
  • 支持讀取表頭、數(shù)據(jù)、異常處理
  • 可監(jiān)聽(tīng)每行數(shù)據(jù)(適合大數(shù)據(jù)量)
EasyExcel.read(fileName, DemoData.class, new MyReadListener())
         .sheet()
         .doRead();

2. 寫(xiě) Excel

  • 自動(dòng)根據(jù)對(duì)象字段生成表頭和數(shù)據(jù)
  • 支持大數(shù)據(jù)量分批寫(xiě)入(百萬(wàn)行無(wú)壓力)
  • 可指定列寬、行高、樣式等
EasyExcel.write(fileName, DemoData.class)
         .sheet("Sheet1")
         .doWrite(dataList);

3. Excel 填充(模板填充)

  • 提供 Excel 模板(帶占位符如 {name}
  • 自動(dòng)填充數(shù)據(jù),適合生成報(bào)表、證書(shū)等
EasyExcel.write(out, DemoData.class)
         .withTemplate(templateFile)
         .sheet()
         .doFill(data);

4. 豐富的注解支持

  • @ExcelProperty("姓名"):指定列名
  • @ColumnWidth(20):設(shè)置列寬
  • @DateTimeFormat("yyyy-MM-dd"):日期格式化
  • @NumberFormat("#,##0.00"):數(shù)字格式化
  • @ExcelIgnore:忽略字段
public class User {
    @ExcelProperty("用戶姓名")
    private String name;

    @ExcelProperty("注冊(cè)時(shí)間")
    @DateTimeFormat("yyyy年MM月dd日")
    private Date registerTime;
}

二、 快速開(kāi)始

1. 引入依賴

<!--Lombok依賴-->  
<dependency>  
    <groupId>org.projectlombok</groupId>  
    <artifactId>lombok</artifactId>  
    <optional>true</optional>  
</dependency>  
<!--easyexcel依賴-->  
<dependency>  
    <groupId>com.alibaba</groupId>  
    <artifactId>easyexcel</artifactId>  
    <version>4.0.3</version>  
</dependency>  
<!--fastjson依賴-->  
<dependency>  
    <groupId>com.alibaba</groupId>  
    <artifactId>fastjson</artifactId>  
    <version>2.0.57</version>  
</dependency>

2. 寫(xiě)實(shí)體類(lèi)

package com.nuc.demoexcel.entity;  
  
import com.alibaba.excel.annotation.ExcelIgnore;  
import com.alibaba.excel.annotation.ExcelProperty;  
import com.alibaba.excel.annotation.write.style.ColumnWidth;  
import com.alibaba.excel.annotation.write.style.HeadRowHeight;  
import com.alibaba.fastjson.annotation.JSONField;  
import lombok.Data;  
  
import java.util.Date;  
  
@Data  
@HeadRowHeight(20)  
@ColumnWidth(20)  
public class DemoDate {  
    @ExcelProperty("字符串標(biāo)題")  
    private String name;  
    @ExcelProperty("日期標(biāo)題")  
    @JSONField(format = "yyyy-MM-dd,HH:mm:ss") // 格式化日期 沒(méi)有注解會(huì)默認(rèn)轉(zhuǎn)成時(shí)間戳  
    private Date date;  
    @ExcelProperty("數(shù)字標(biāo)題")  
    private Double doubleData;  
    @ExcelIgnore  
    private String ignore;  
}

3. Excel讀取

讀取常用有三種方法

  • 這是我提前準(zhǔn)備好的excel文件

方法一:

有封裝的實(shí)體類(lèi)
.head(DemoData.class)封裝
結(jié)果用List<DemoData>

/**  
 * 測(cè)試讀1  
 * 有封裝的實(shí)體類(lèi)  
 */  
@Test  
void readExcel1() {  
    System.out.println("測(cè)試");  
    List<DemoDate> list = EasyExcel.read("D:\\測(cè)試\\demo.xlsx").head(DemoDate.class).sheet().doReadSync();  
    list.forEach(data ->{  
        String jsonString = JSON.toJSONString(data);  
        log.info("讀取到數(shù)據(jù){}",jsonString);  
    });  
}
  • 測(cè)試結(jié)果

方法二:沒(méi)有封裝的實(shí)體類(lèi)

沒(méi)有封裝的實(shí)體類(lèi)
結(jié)果用List<Map<Integer,String>>

/**  
 * 測(cè)試讀2  
 * 沒(méi)有分裝的實(shí)體類(lèi)用Map來(lái)存  
 */  
@Test  
void readExcel2() {  
    System.out.println("測(cè)試");  
    List<Map<Integer,String>> list = EasyExcel.read("D:\\測(cè)試\\demo.xlsx").sheet().doReadSync();  
    list.forEach(data ->{  
        String string = data.toString();  
        log.info("讀取到數(shù)據(jù){}",string);  
    });  
}
  • 測(cè)試結(jié)果同上

方法三:使用監(jiān)聽(tīng)器來(lái)讀取文件

使用監(jiān)聽(tīng)器來(lái)讀取文件

/**  
 * 測(cè)試讀3  
 * 使用監(jiān)聽(tīng)器讀取文件。  
 * 需要我們額外創(chuàng)建一個(gè)監(jiān)聽(tīng)器  
 */  
@Test  
void readExcel3() {  
    System.out.println("測(cè)試");  
    EasyExcel.read("D:\\測(cè)試\\demo.xlsx", DemoDate.class, new ExcelListener(demoService)).sheet().doRead();  
}
  • 這里需要額外寫(xiě)一個(gè)監(jiān)聽(tīng)器
package com.nuc.demoexcel.listener;  
  
import com.alibaba.excel.context.AnalysisContext;  
import com.alibaba.excel.event.AnalysisEventListener;  
import com.alibaba.fastjson.JSON;  
import com.nuc.demoexcel.entity.DemoDate;  
import com.nuc.demoexcel.service.DemoService;  
import lombok.RequiredArgsConstructor;  
import lombok.extern.slf4j.Slf4j;  
  
import java.util.ArrayList;  
import java.util.List;  
  
/**  
 * 這里不能用SprongIoC容器來(lái)管理Listener,Listener可添加多個(gè),會(huì)自動(dòng)執(zhí)行  
 */  
@Slf4j  
@RequiredArgsConstructor  
public class ExcelListener extends AnalysisEventListener<DemoDate> {  
  
    /**  
     * 每隔5條存儲(chǔ)數(shù)據(jù)庫(kù),清理list,方便內(nèi)存回收  
     */  
    private static final int BATCH_COUNT = 5;  
    private List<DemoDate> list = new ArrayList<DemoDate>();  
    private final DemoService demoService;  
  
    @Override  
    public void invoke(DemoDate demoDate, AnalysisContext analysisContext) {  
        log.info("讀取到數(shù)據(jù){}", JSON.toJSONString(demoDate));  
        list.add(demoDate);  
        // 到達(dá)批次數(shù),保存數(shù)據(jù),防止數(shù)據(jù)幾萬(wàn)條數(shù)據(jù)在內(nèi)存中,容易OOM  
        if (list.size() >= BATCH_COUNT) {  
            saveData();  
            // 保存完成,清理list  
            list.clear();  
        }  
  
    }  
  
  
    /**  
     * 所有的數(shù)據(jù)都讀完,最后收尾數(shù)據(jù)  
     * @param analysisContext  
     */  
    @Override  
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {  
        //這里也要保存數(shù)據(jù),確保最后遺留的數(shù)據(jù)也存儲(chǔ)到數(shù)據(jù)庫(kù)  
        saveData();  
        log.info("所有數(shù)據(jù)解析完成!");  
    }  
  
    /**  
     * 存儲(chǔ)數(shù)據(jù)到數(shù)據(jù)庫(kù)  
     */  
    private void saveData() {  
        log.info("{}條數(shù)據(jù),開(kāi)始存儲(chǔ)數(shù)據(jù)庫(kù)!", list.size());  
        demoService.saveData(list);  
        log.info("存儲(chǔ)數(shù)據(jù)庫(kù)成功!");  
    }  
}

這里只是演示EasyExcel
所以就不寫(xiě)Service層代碼

4. Excel寫(xiě)入

  
/**  
 * 測(cè)試寫(xiě)  
 */  
@Test  
void testWrite() {  
    // 文件輸出位置  
    String fileName = "D:\\text" + System.currentTimeMillis() + ".xlsx";  
    EasyExcel.write(fileName, DemoDate.class).sheet("模板").doWrite(data());  
  
  
  
}  
  
private List<DemoDate> data(){  
    //模擬數(shù)據(jù)  
    List<DemoDate> list = new ArrayList<>();  
    for (int i = 0; i < 13; i++) {  
        DemoDate data = new DemoDate();  
        data.setName("張三"+i);  
        data.setDate(new Date());  
        data.setDoubleData(Math.random()*100);  
        list.add(data);  
    }  
    return list;  
}
  • 成功寫(xiě)入

5. Excel下載&上傳

package com.nuc.demoexcel.controller;  
  
import com.alibaba.excel.EasyExcel;  
import com.alibaba.fastjson.JSON;  
import com.nuc.demoexcel.entity.DemoDate;  
import com.nuc.demoexcel.listener.ExcelListener;  
import com.nuc.demoexcel.service.DemoService;  
import jakarta.servlet.http.HttpServletResponse;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.PostMapping;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.ResponseBody;  
import org.springframework.web.multipart.MultipartFile;  
  
import java.io.IOException;  
import java.net.URLEncoder;  
import java.util.*;  
  
@Controller  
public class UploadDownController {  
  
    @Autowired  
    private DemoService demoService;  
  
    /**  
     * 下載失敗了(會(huì)返回一個(gè)有部分?jǐn)?shù)據(jù)的Excel)  
     * @param response  
     * @throws IOException  
     */    @RequestMapping("/download")  
    public void download(HttpServletResponse  response) throws IOException {  
        response.setContentType("application/vnd.ms-excel");  
        response.setCharacterEncoding("utf-8");  
        //這里的encode是解決中文亂碼, 這里必須使用UTF-8, 不然亂碼  
        String fileName = URLEncoder.encode("測(cè)試", "UTF-8").replaceAll("\\+", "%20");  
        //這里使用附件的形式下載  
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");  
        EasyExcel.write(response.getOutputStream(), DemoDate.class).sheet("模板").doWrite(data());  
    }  
  
  
    @RequestMapping("/api/download")  
    public void downloadApi(HttpServletResponse response) throws IOException {  
        try {  
            response.setContentType("application/vnd.ms-excel");  
            response.setCharacterEncoding("utf-8");  
            //這里的encode是解決中文亂碼, 這里必須使用UTF-8, 不然亂碼  
            String fileName = URLEncoder.encode("測(cè)試", "UTF-8").replaceAll("\\+", "%20");  
            //這里使用附件的形式下載  
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");  
            EasyExcel.write(response.getOutputStream(), DemoDate.class).sheet("模板").doWrite(data());  
        } catch (IOException e) {  
            //重置響應(yīng)  
            response.reset();  
            response.setContentType("application/json");  
            response.setCharacterEncoding("utf-8");  
            Map<String, Object> map = new HashMap<>();  
            map.put("status", 500);  
            map.put("message", "數(shù)據(jù)寫(xiě)入失敗" + e.getMessage());  
            response.getWriter().println(JSON.toJSONString(map));  
        }  
  
    }  
  
    @PostMapping("/upload")  
    @ResponseBody  
    public String upload(MultipartFile file) throws IOException {  
        EasyExcel.read(file.getInputStream(), DemoDate.class, new ExcelListener(demoService)).sheet().doRead();  
        return "success";  
    }  
  
    private List<DemoDate> data(){  
        //模擬數(shù)據(jù)  
        List<DemoDate> list = new ArrayList<>();  
        for (int i = 0; i < 13; i++) {  
            DemoDate data = new DemoDate();  
            data.setName("張三"+i);  
            data.setDate(new Date());  
            data.setDoubleData(Math.random()*100);  
            list.add(data);  
        }  
        return list;  
    }  
}
  • 調(diào)用localhost:8080/download接口

5. Excel下載&上傳

package com.nuc.demoexcel.controller;  
  
import com.alibaba.excel.EasyExcel;  
import com.alibaba.fastjson.JSON;  
import com.nuc.demoexcel.entity.DemoDate;  
import com.nuc.demoexcel.listener.ExcelListener;  
import com.nuc.demoexcel.service.DemoService;  
import jakarta.servlet.http.HttpServletResponse;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.PostMapping;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.ResponseBody;  
import org.springframework.web.multipart.MultipartFile;  
  
import java.io.IOException;  
import java.net.URLEncoder;  
import java.util.*;  
  
@Controller  
public class UploadDownController {  
  
    @Autowired  
    private DemoService demoService;  
  
    /**  
     * 下載失敗了(會(huì)返回一個(gè)有部分?jǐn)?shù)據(jù)的Excel)  
     * @param response  
     * @throws IOException  
     */    @RequestMapping("/download")  
    public void download(HttpServletResponse  response) throws IOException {  
        response.setContentType("application/vnd.ms-excel");  
        response.setCharacterEncoding("utf-8");  
        //這里的encode是解決中文亂碼, 這里必須使用UTF-8, 不然亂碼  
        String fileName = URLEncoder.encode("測(cè)試", "UTF-8").replaceAll("\\+", "%20");  
        //這里使用附件的形式下載  
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");  
        EasyExcel.write(response.getOutputStream(), DemoDate.class).sheet("模板").doWrite(data());  
    }  
  
  
    @RequestMapping("/api/download")  
    public void downloadApi(HttpServletResponse response) throws IOException {  
        try {  
            response.setContentType("application/vnd.ms-excel");  
            response.setCharacterEncoding("utf-8");  
            //這里的encode是解決中文亂碼, 這里必須使用UTF-8, 不然亂碼  
            String fileName = URLEncoder.encode("測(cè)試", "UTF-8").replaceAll("\\+", "%20");  
            //這里使用附件的形式下載  
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");  
            EasyExcel.write(response.getOutputStream(), DemoDate.class).sheet("模板").doWrite(data());  
        } catch (IOException e) {  
            //重置響應(yīng)  
            response.reset();  
            response.setContentType("application/json");  
            response.setCharacterEncoding("utf-8");  
            Map<String, Object> map = new HashMap<>();  
            map.put("status", 500);  
            map.put("message", "數(shù)據(jù)寫(xiě)入失敗" + e.getMessage());  
            response.getWriter().println(JSON.toJSONString(map));  
        }  
  
    }  
  
    @PostMapping("/upload")  
    @ResponseBody  
    public String upload(MultipartFile file) throws IOException {  
        EasyExcel.read(file.getInputStream(), DemoDate.class, new ExcelListener(demoService)).sheet().doRead();  
        return "success";  
    }  
  
    private List<DemoDate> data(){  
        //模擬數(shù)據(jù)  
        List<DemoDate> list = new ArrayList<>();  
        for (int i = 0; i < 13; i++) {  
            DemoDate data = new DemoDate();  
            data.setName("張三"+i);  
            data.setDate(new Date());  
            data.setDoubleData(Math.random()*100);  
            list.add(data);  
        }  
        return list;  
    }  
}
  • 調(diào)用localhost:8080/download接口

  • 調(diào)用localhost:8080/api/upload接口

  • 使用postman 調(diào)用localhost:8080/upload接口

    • 后端

總結(jié) 

到此這篇關(guān)于EasyExcel讀寫(xiě)、模板填充、Web上傳下載入門(mén)到實(shí)戰(zhàn)的文章就介紹到這了,更多相關(guān)EasyExcel讀寫(xiě)、模板填充、Web上傳下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

清河县| 西丰县| 青神县| 荣成市| 汽车| 扶绥县| 东乌珠穆沁旗| 昌平区| 乐平市| 辉南县| 桂平市| 宁南县| 湖北省| 灵川县| 苗栗县| 鲜城| 察隅县| 海南省| 新河县| 习水县| 伽师县| 大余县| 鹤庆县| 民乐县| 和静县| 梁河县| 康马县| 芜湖市| 珠海市| 集贤县| 桓台县| 汉阴县| 阿城市| 新竹县| 武乡县| 华安县| 五华县| 万年县| 雅安市| 宣城市| 北宁市|