Java表數(shù)據(jù)導(dǎo)出到Excel中的實(shí)現(xiàn)
工作中遇到的需求,菜鳥筆記
1.pom中導(dǎo)入所用依賴
<!--導(dǎo)出Excel--> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>3.0.3</version> </dependency>
2.創(chuàng)建一個(gè)實(shí)體類來(lái)接收從數(shù)據(jù)庫(kù)查出的數(shù)據(jù)
- 我用到了兩個(gè)表中的數(shù)據(jù),所以我把需要的字段提取出來(lái)寫了一個(gè)類
- @ExcelTarget(value = “”)就標(biāo)識(shí)該類要導(dǎo)出為Excel,或者導(dǎo)入Excel的接收對(duì)象。value值其標(biāo)識(shí)作用,不重復(fù)即可,隨便取。
- @Excel(name = “”)就是作用于類屬性上,表示該屬性對(duì)應(yīng)Excel的哪一列,即name的值,當(dāng)然還有其它屬性。
package com.school.grid.business.domain.exam; import cn.afterturn.easypoi.excel.annotation.ExcelTarget; import cn.afterturn.easypoi.excel.annotation.Excel; import lombok.Data; /** * @author song * 導(dǎo)出成績(jī)實(shí)體 */ @Data @ExcelTarget("exportGrades") public class ExportGrades { /** * 學(xué)員名字 */ @Excel(name = "學(xué)員名字") private String userName; /** * 學(xué)員身份證號(hào) */ @Excel(name = "學(xué)員身份證號(hào)") private String userIdCardNum; /** * 學(xué)員手機(jī)號(hào) */ @Excel(name = "學(xué)員手機(jī)號(hào)") private String userId; /** * 考試科目 */ @Excel(name = "考試科目") private String paperManageSubject; /** * 學(xué)員考試成績(jī) */ @Excel(name = "學(xué)員考試成績(jī)") private String userInfoPaperTtoalScore; }
3.Controller層
/** * 導(dǎo)出:姓名、身份證、電話、考試科目、成績(jī) * @param response * @param * @throws IOException */ @ApiOperation("成績(jī)導(dǎo)出到excel") @GetMapping("/export/grades") public void detailsImport(HttpServletResponse response) throws IOException { // 告訴瀏覽器用什么軟件可以打開此文件 response.setHeader("content-Type", "application/vnd.ms-excel"); // 下載文件的默認(rèn)名稱 response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("學(xué)員成績(jī)表導(dǎo)出", StandardCharsets.UTF_8) + ".xls"); //編碼 response.setCharacterEncoding("UTF-8"); //查詢數(shù)據(jù)庫(kù)要導(dǎo)出的數(shù)據(jù) List<ExportGrades> list = certificateService.exportGrades(); //該方法需要三個(gè)參數(shù):1、需要一個(gè)ExportParams2、要導(dǎo)出實(shí)體類3、導(dǎo)出的數(shù)據(jù) Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(),ExportGrades.class,list); workbook.write(response.getOutputStream()); }
4.sql要查詢的數(shù)據(jù),我用的到兩個(gè)表中的數(shù)據(jù),所以我第二步就新建一個(gè)實(shí)體類來(lái)接收查詢出來(lái)的數(shù)據(jù)
到此這篇關(guān)于Java表數(shù)據(jù)導(dǎo)出到Excel中的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java表導(dǎo)出到Excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)重定向過(guò)程中添加請(qǐng)求頭信息
在Java中,我們經(jīng)常需要使用網(wǎng)絡(luò)請(qǐng)求來(lái)與服務(wù)器進(jìn)行通信,在進(jìn)行網(wǎng)絡(luò)請(qǐng)求時(shí),有時(shí)我們需要在重定向過(guò)程中添加請(qǐng)求頭信息,本文將介紹如何使用Java在重定向過(guò)程中添加請(qǐng)求頭,并提供相應(yīng)的代碼示例,2023-10-10SpringBoot@Componet注解注入失敗的問(wèn)題
這篇文章主要介紹了SpringBoot@Componet注解注入失敗的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03JDBC連接MySQL數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)過(guò)程詳解
這篇文章主要介紹了JDBC連接MySQL數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11logback標(biāo)記日志過(guò)濾器MarkerFilter源碼解讀
這篇文章主要為大家介紹了logback標(biāo)記日志過(guò)濾器MarkerFilter源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11tomcat報(bào)錯(cuò):Wrapper cannot find servlet class ...問(wèn)題解決
這篇文章主要介紹了tomcat報(bào)錯(cuò):Wrapper cannot find servlet class ...問(wèn)題解決的相關(guān)資料,需要的朋友可以參考下2016-11-11