Java表數(shù)據(jù)導(dǎo)出到Excel中的實現(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)建一個實體類來接收從數(shù)據(jù)庫查出的數(shù)據(jù)
- 我用到了兩個表中的數(shù)據(jù),所以我把需要的字段提取出來寫了一個類
- @ExcelTarget(value = “”)就標(biāo)識該類要導(dǎo)出為Excel,或者導(dǎo)入Excel的接收對象。value值其標(biāo)識作用,不重復(fù)即可,隨便取。
- @Excel(name = “”)就是作用于類屬性上,表示該屬性對應(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)出成績實體
*/
@Data
@ExcelTarget("exportGrades")
public class ExportGrades {
/**
* 學(xué)員名字
*/
@Excel(name = "學(xué)員名字")
private String userName;
/**
* 學(xué)員身份證號
*/
@Excel(name = "學(xué)員身份證號")
private String userIdCardNum;
/**
* 學(xué)員手機(jī)號
*/
@Excel(name = "學(xué)員手機(jī)號")
private String userId;
/**
* 考試科目
*/
@Excel(name = "考試科目")
private String paperManageSubject;
/**
* 學(xué)員考試成績
*/
@Excel(name = "學(xué)員考試成績")
private String userInfoPaperTtoalScore;
}
3.Controller層
/**
* 導(dǎo)出:姓名、身份證、電話、考試科目、成績
* @param response
* @param
* @throws IOException
*/
@ApiOperation("成績導(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é)員成績表導(dǎo)出", StandardCharsets.UTF_8) + ".xls");
//編碼
response.setCharacterEncoding("UTF-8");
//查詢數(shù)據(jù)庫要導(dǎo)出的數(shù)據(jù)
List<ExportGrades> list = certificateService.exportGrades();
//該方法需要三個參數(shù):1、需要一個ExportParams2、要導(dǎo)出實體類3、導(dǎo)出的數(shù)據(jù)
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(),ExportGrades.class,list);
workbook.write(response.getOutputStream());
}4.sql要查詢的數(shù)據(jù),我用的到兩個表中的數(shù)據(jù),所以我第二步就新建一個實體類來接收查詢出來的數(shù)據(jù)

到此這篇關(guān)于Java表數(shù)據(jù)導(dǎo)出到Excel中的實現(xiàn)的文章就介紹到這了,更多相關(guān)Java表導(dǎo)出到Excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JDBC連接MySQL數(shù)據(jù)庫批量插入數(shù)據(jù)過程詳解
這篇文章主要介紹了JDBC連接MySQL數(shù)據(jù)庫批量插入數(shù)據(jù)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
logback標(biāo)記日志過濾器MarkerFilter源碼解讀
這篇文章主要為大家介紹了logback標(biāo)記日志過濾器MarkerFilter源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
tomcat報錯:Wrapper cannot find servlet class ...問題解決
這篇文章主要介紹了tomcat報錯:Wrapper cannot find servlet class ...問題解決的相關(guān)資料,需要的朋友可以參考下2016-11-11

