在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;@Controllerpublic 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è)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章: