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