Springboot+Easyexcel將數(shù)據(jù)寫入模板文件并導(dǎo)出Excel的操作代碼
一、導(dǎo)入依賴
<!--操作excel工具包--> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.0.5</version> </dependency>
話費(fèi)電費(fèi)燃?xì)赓M(fèi)94折充值,加v: sz08111,長期有效
二、根據(jù)excel表頭創(chuàng)建對應(yīng)的實(shí)體類Pojo
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@Data
public class ExcelTitle {
@ExcelProperty(value="事件名稱", index=0)
private String eventName;
@ExcelProperty(value="需求負(fù)責(zé)人", index=1)
private String prdManager;
@ExcelProperty(value="技術(shù)負(fù)責(zé)人", index=2)
private String techManager;
@ExcelProperty(value="文檔鏈接", index=3)
private String prdDocs;
@ExcelProperty(value="數(shù)據(jù)鏈接", index=4)
private String statsDocs;
@ExcelProperty(value="統(tǒng)計(jì)口徑", index=5)
private String reportCaliber;這里采用了@ExcelProperty的注解,其中value表示列名,index表示列名的索引值。
三、Controller類接收請求
@RequestMapping(value = "/bulkOutput", method = RequestMethod.GET)
public ResultBean bulkOutput(HttpServletResponse response) {
// 重要! 設(shè)置返回格式是excel形式
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
// 設(shè)置編碼格式
response.setCharacterEncoding("utf-8");
// 設(shè)置URLEncoder.encode 防止中文亂碼
String fileName = null;
try {
fileName = URLEncoder.encode("數(shù)據(jù)批量導(dǎo)出", "UTF-8").replaceAll("\\+", "%20");
// 設(shè)置響應(yīng)頭
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
List<ExcelTitle> bulkOutputData = wildEventService.getBulkOutputData();
// 模板文件保存在springboot項(xiàng)目的resources/static下
Resource resource = new ClassPathResource("static/數(shù)據(jù)批量導(dǎo)出模板.xlsx");
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
.withTemplate(resource.getInputStream()) // 利用模板的輸出流
.build();
// 寫入模板文件的第一個(gè)sheet 索引0
WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
// 將數(shù)據(jù)寫入到模板文件的對應(yīng)sheet中
excelWriter.write(bulkOutputData, writeSheet);
excelWriter.finish();
} catch (UnsupportedEncodingException e) {
return ResultBean.errorService(e.getMessage());
} catch (IOException e) {
return ResultBean.errorService(e.getMessage());
}
return ResultBean.success("數(shù)據(jù)導(dǎo)出成功!");
}上述代碼中,首先對response進(jìn)行了設(shè)置,設(shè)置了返回類型,響應(yīng)頭,以及導(dǎo)出下載時(shí)的文件名稱。接下來,利用Resource resource = new ClassPathResource(“static/數(shù)據(jù)批量導(dǎo)出模板.xlsx”); 讀取項(xiàng)目下的模板文件,并調(diào)用easyexcel的寫入方法。這里write(response.getOutputStream())表示寫入response的輸出流,即將文件返回給客戶端進(jìn)行下載。withTemplate(resource.getInputStream())表示讀取模板文件進(jìn)行寫入。最后調(diào)用 WriteSheet writeSheet = EasyExcel.writerSheet(0).build();將蘇劇寫入模板文件的第一個(gè)sheet中(索引從0開始)。
四、Service層獲取待寫入數(shù)據(jù)
import com.example.demo.Pojo.ExcelTitle;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class BulkOutputService {
public List<ExcelTitle> getOutputData()
{
List<ExcelTitle> resultList = new ArrayList<>();
// 第一行數(shù)據(jù)
ExcelTitle t1 = new ExcelTitle();
t1.setEventName("測試數(shù)據(jù)1");
t1.setTechManager("張三");
t1.setPrdManager("張三");
t1.setPrdDocs("http://prdDocs.com");
t1.setStatsDocs("http://statsDocs.com");
t1.setReportCaliber("reportCaliber");
// 第二行數(shù)據(jù)
ExcelTitle t2 = new ExcelTitle();
t2.setEventName("測試數(shù)據(jù)2");
t2.setTechManager("李四");
t2.setPrdManager("李四");
t2.setPrdDocs("http://prdDocs.com");
t2.setStatsDocs("http://statsDocs.com");
t2.setReportCaliber("reportCaliber");
resultList.add(t1);
resultList.add(t2);
return resultList;
}
}五、效果展示
啟動springboot程序,在瀏覽器中輸入請求: http://localhost:8080/bulkOutput,可以發(fā)現(xiàn)返回的excel文件被瀏覽器下載,打開后內(nèi)容如下:

可以看到,數(shù)據(jù)根據(jù)模板格式寫入了文件,并以excel的形式導(dǎo)出。初步完成了我們預(yù)定的場景。
六、總結(jié)
本次實(shí)現(xiàn)的功能是利用現(xiàn)有模板,將數(shù)據(jù)批量導(dǎo)出成excel,借助于easyexcel來實(shí)現(xiàn)操作excel的功能。最重要的功能模塊是Controller里的內(nèi)容。
需要注意
- 需要加上response的響應(yīng)類型和響應(yīng)頭來使得返回請求返回excel文件
- 寫到Web流時(shí),這里的ContentType和CharacterEncoding不要亂碼,否則很容易亂碼或者文件損壞
- 使用EasyExcel.withTemplate引入模板的輸入流
到此這篇關(guān)于Springboot+Easyexcel將數(shù)據(jù)寫入模板文件并導(dǎo)出Excel的文章就介紹到這了,更多相關(guān)Springboot Easyexcel 導(dǎo)出Excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot種如何使用?EasyExcel?實(shí)現(xiàn)自定義表頭導(dǎo)出并實(shí)現(xiàn)數(shù)據(jù)格式化轉(zhuǎn)換
- SpringBoot中使用EasyExcel并行導(dǎo)出多個(gè)excel文件并壓縮zip后下載的代碼詳解
- Springboot整合EasyExcel實(shí)現(xiàn)Excel文件上傳方式
- SpringBoot?整合?EasyExcel?實(shí)現(xiàn)自由導(dǎo)入導(dǎo)出功能
- SpringBoot整合EasyExcel實(shí)現(xiàn)批量導(dǎo)入導(dǎo)出
- SpringBoot整合EasyExcel實(shí)現(xiàn)復(fù)雜Excel表格的導(dǎo)入導(dǎo)出
- SpringBoot整合EasyExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出功能
- 使用SpringBoot+EasyExcel+Vue實(shí)現(xiàn)excel表格的導(dǎo)入和導(dǎo)出詳解
相關(guān)文章
SpringBoot實(shí)現(xiàn)過濾器攔截器的耗時(shí)對比
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)過濾器攔截器的輸出接口耗時(shí)對比,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-06-06
REST架構(gòu)及RESTful應(yīng)用程序簡介
這篇文章主要為大家介紹了REST架構(gòu)及RESTful的應(yīng)用程序簡介,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
利用EasyPOI實(shí)現(xiàn)多sheet和列數(shù)的動態(tài)生成
EasyPoi功能如同名字,主打的功能就是容易,讓一個(gè)沒見接觸過poi的人員就可以方便的寫出Excel導(dǎo)出,Excel導(dǎo)入等功能,本文主要來講講如何利用EasyPOI實(shí)現(xiàn)多sheet和列數(shù)的動態(tài)生成,需要的可以了解下2025-03-03
關(guān)于spring?boot使用?jdbc+mysql?連接的問題
這篇文章主要介紹了spring?boot使用?jdbc+mysql?連接,在這里mysql?8.x版本驅(qū)動包,要使用?com.mysql.cj.jdbc.Driver作為驅(qū)動類,文中給大家詳細(xì)介紹,需要的朋友可以參考下2022-03-03
Springboot內(nèi)外部logback多環(huán)境配置詳解
本文主要介紹了Springboot內(nèi)外部logback多環(huán)境配置詳解,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
詳解Java Web項(xiàng)目啟動執(zhí)行順序
這篇文章主要介紹了詳解Java Web項(xiàng)目啟動執(zhí)行順序,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06

