在SpringBoot: SpringBoot里面創(chuàng)建導(dǎo)出Excel的接口教程
在Web項(xiàng)目中,難免需要導(dǎo)出Excel這樣的功能,后端接口怎么實(shí)現(xiàn)呢,Controller代碼在下面,復(fù)制到項(xiàng)目的Controller中即可使用:
首先加入Excel的依賴,本例中我們用apache的poi:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency>
后臺(tái)導(dǎo)出Excel的Controller接口代碼:
import org.apache.poi.hssf.usermodel.*; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Controller public class ExcelController { /** * Excel表格導(dǎo)出接口 * http://localhost:8080/ExcelDownload * @param response response對(duì)象 * @throws IOException 拋IO異常 */ @RequestMapping("/ExcelDownload") public void excelDownload(HttpServletResponse response) throws IOException { //表頭數(shù)據(jù) String[] header = {"ID", "姓名", "性別", "年齡", "地址", "分?jǐn)?shù)"}; //數(shù)據(jù)內(nèi)容 String[] student1 = {"1", "小紅", "女", "23", "成都青羊區(qū)", "96"}; String[] student2 = {"2", "小強(qiáng)", "男", "26", "成都金牛區(qū)", "91"}; String[] student3 = {"3", "小明", "男", "28", "成都武侯區(qū)", "90"}; //聲明一個(gè)工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); //生成一個(gè)表格,設(shè)置表格名稱為"學(xué)生表" HSSFSheet sheet = workbook.createSheet("學(xué)生表"); //設(shè)置表格列寬度為10個(gè)字節(jié) sheet.setDefaultColumnWidth(10); //創(chuàng)建第一行表頭 HSSFRow headrow = sheet.createRow(0); //遍歷添加表頭(下面模擬遍歷學(xué)生,也是同樣的操作過程) for (int i = 0; i < header.length; i++) { //創(chuàng)建一個(gè)單元格 HSSFCell cell = headrow.createCell(i); //創(chuàng)建一個(gè)內(nèi)容對(duì)象 HSSFRichTextString text = new HSSFRichTextString(header[i]); //將內(nèi)容對(duì)象的文字內(nèi)容寫入到單元格中 cell.setCellValue(text); } //模擬遍歷結(jié)果集,把內(nèi)容加入表格 //模擬遍歷第一個(gè)學(xué)生 HSSFRow row1 = sheet.createRow(1); for (int i = 0; i < student1.length; i++) { HSSFCell cell = row1.createCell(i); HSSFRichTextString text = new HSSFRichTextString(student1[i]); cell.setCellValue(text); } //模擬遍歷第二個(gè)學(xué)生 HSSFRow row2 = sheet.createRow(2); for (int i = 0; i < student2.length; i++) { HSSFCell cell = row2.createCell(i); HSSFRichTextString text = new HSSFRichTextString(student2[i]); cell.setCellValue(text); } //模擬遍歷第三個(gè)學(xué)生 HSSFRow row3 = sheet.createRow(3); for (int i = 0; i < student3.length; i++) { HSSFCell cell = row3.createCell(i); HSSFRichTextString text = new HSSFRichTextString(student3[i]); cell.setCellValue(text); } //準(zhǔn)備將Excel的輸出流通過response輸出到頁面下載 //八進(jìn)制輸出流 response.setContentType("application/octet-stream"); //這后面可以設(shè)置導(dǎo)出Excel的名稱,此例中名為student.xls response.setHeader("Content-disposition", "attachment;filename=student.xls"); //刷新緩沖 response.flushBuffer(); //workbook將Excel寫入到response的輸出流中,供頁面下載 workbook.write(response.getOutputStream()); } }
然后訪問接口,彈出頁面:
下載該Excel,打開后如下圖:
至此為止,SpringBoot的后臺(tái)Controller接口導(dǎo)出Excel數(shù)據(jù)表,已成功實(shí)現(xiàn)!
后來我封裝了一個(gè)靜態(tài)方法,可以在項(xiàng)目中作為工具類使用:
import org.apache.poi.hssf.usermodel.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; /** * Excel工具類 */ public class ExcelUtil { /** * Excel表格導(dǎo)出 * @param response HttpServletResponse對(duì)象 * @param excelData Excel表格的數(shù)據(jù),封裝為L(zhǎng)ist<List<String>> * @param sheetName sheet的名字 * @param fileName 導(dǎo)出Excel的文件名 * @param columnWidth Excel表格的寬度,建議為15 * @throws IOException 拋IO異常 */ public static void exportExcel(HttpServletResponse response, List<List<String>> excelData, String sheetName, String fileName, int columnWidth) throws IOException { //聲明一個(gè)工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); //生成一個(gè)表格,設(shè)置表格名稱 HSSFSheet sheet = workbook.createSheet(sheetName); //設(shè)置表格列寬度 sheet.setDefaultColumnWidth(columnWidth); //寫入List<List<String>>中的數(shù)據(jù) int rowIndex = 0; for(List<String> data : excelData){ //創(chuàng)建一個(gè)row行,然后自增1 HSSFRow row = sheet.createRow(rowIndex++); //遍歷添加本行數(shù)據(jù) for (int i = 0; i < data.size(); i++) { //創(chuàng)建一個(gè)單元格 HSSFCell cell = row.createCell(i); //創(chuàng)建一個(gè)內(nèi)容對(duì)象 HSSFRichTextString text = new HSSFRichTextString(data.get(i)); //將內(nèi)容對(duì)象的文字內(nèi)容寫入到單元格中 cell.setCellValue(text); } } //準(zhǔn)備將Excel的輸出流通過response輸出到頁面下載 //八進(jìn)制輸出流 response.setContentType("application/octet-stream"); //設(shè)置導(dǎo)出Excel的名稱 response.setHeader("Content-disposition", "attachment;filename=" + fileName); //刷新緩沖 response.flushBuffer(); //workbook將Excel寫入到response的輸出流中,供頁面下載該Excel文件 workbook.write(response.getOutputStream()); //關(guān)閉workbook workbook.close(); } }
以上方法調(diào)用示例:
/** * Excel表格導(dǎo)出接口 * http://localhost:8080/ExcelDownload * @param response response對(duì)象 * @throws IOException 拋IO異常 */ @RequestMapping("/ExcelDownload") public void excelDownload(HttpServletResponse response) throws IOException { List<List<String>> excelData = new ArrayList<>(); List<String> head = new ArrayList<>(); head.add("第一列"); head.add("第二列"); head.add("第三列"); List<String> data1 = new ArrayList<>(); data1.add("123"); data1.add("234"); data1.add("345"); List<String> data2 = new ArrayList<>(); data2.add("abc"); data2.add("bcd"); data2.add("cde"); excelData.add(head); excelData.add(data1); excelData.add(data2); String sheetName = "測(cè)試"; String fileName = "ExcelTest.xls"; ExcelUtil.exportExcel(response, excelData, sheetName, fileName, 15); }
以上這篇在SpringBoot: SpringBoot里面創(chuàng)建導(dǎo)出Excel的接口教程就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot中EasyExcel實(shí)現(xiàn)Excel文件的導(dǎo)入導(dǎo)出
- SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出
- SpringBoot整合EasyExcel實(shí)現(xiàn)文件導(dǎo)入導(dǎo)出
- SpringBoot集成EasyExcel實(shí)現(xiàn)Excel導(dǎo)入的方法
- SpringBoot內(nèi)存數(shù)據(jù)導(dǎo)出成Excel的實(shí)現(xiàn)方法
- SpringBoot實(shí)現(xiàn)Excel讀取的實(shí)例教程
- 使用Springboot+poi上傳并處理百萬級(jí)數(shù)據(jù)EXCEL
- SpringBoot讀取excel表格的示例代碼
- 解決springboot 多線程使用MultipartFile讀取excel文件內(nèi)容報(bào)錯(cuò)問題
- SpringBoot中使用JeecgBoot的Autopoi導(dǎo)出Excel的方法步驟
- SpringBoot整合POI導(dǎo)出通用Excel的方法示例
- Springboot POI導(dǎo)出Excel(瀏覽器)
- 基于SpringBoot框架管理Excel和PDF文件類型
- SpringBoot實(shí)現(xiàn)Excel文件批量上傳導(dǎo)入數(shù)據(jù)庫
- Springboot實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel的方法
相關(guān)文章
淺談Java序列化和反序列化為何要實(shí)現(xiàn)Serializable接口
這篇文章主要介紹了淺談Java序列化和反序列化為何要實(shí)現(xiàn)Serializable接口,序列化最重要的作用是在傳遞和保存對(duì)象時(shí).保證對(duì)象的完整性和可傳遞性,對(duì)象轉(zhuǎn)換為有序字節(jié)流,以便在網(wǎng)絡(luò)上傳輸或者保存在本地文件中,需要的朋友可以參考下2023-12-12透徹理解Java中Synchronized(對(duì)象鎖)和Static Synchronized(類鎖)的區(qū)別
這篇文章主要介紹了Java中Synchronized(對(duì)象鎖)和Static Synchronized(類鎖)的區(qū)別,希望對(duì)大家有所幫助,一起跟隨小編過來看看吧2018-05-05詳解Java利用實(shí)現(xiàn)對(duì)稱加密(DES、3DES、AES)
本篇文章主要介紹了Java利用實(shí)現(xiàn)對(duì)稱加密(DES、3DES、AES),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01springboot3請(qǐng)求參數(shù)種類及接口測(cè)試案例小結(jié)
這篇文章主要介紹了springboot3請(qǐng)求參數(shù)種類及接口測(cè)試案例小結(jié),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-10-10