色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

在SpringBoot: SpringBoot里面創(chuàng)建導(dǎo)出Excel的接口教程

瀏覽:106日期:2022-06-16 17:20:22

在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()); }}

然后訪問接口,彈出頁面:

在SpringBoot: SpringBoot里面創(chuàng)建導(dǎo)出Excel的接口教程

下載該Excel,打開后如下圖:

在SpringBoot: SpringBoot里面創(chuàng)建導(dǎo)出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ù),封裝為List<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的接口教程就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: excel
相關(guān)文章:
主站蜘蛛池模板: 亚洲天堂男人在线 | 国产美女高清一级a毛片 | 成年人免费网站视频 | 久久久久欧美情爱精品 | 欧美另类在线视频 | 久久精品99精品免费观看 | 国产精品高清全国免费观看 | 日本免费网址 | 成人在线视频免费观看 | 免费看岛国视频在线观看 | 手机毛片 | 国产成人精品曰本亚洲77美色 | 香蕉国产人午夜视频在线观看 | 国产高清一区二区三区四区 | 国产主播大尺度精品福利 | 好湿好紧好痛a级是免费视频 | 成人毛片免费播放 | 亚洲rct中文字幕在线 | 美女视频黄色在线观看 | 亚洲国产精品自在现线让你爽 | 久久99久久精品国产99热 | 三上悠亚免费一区二区在线 | 欧美一级日韩在线观看 | 寡妇一级a毛片免费播放 | 亚洲影院手机版777点击进入影院 | 男人的天堂在线观看入口 | 日本亚洲免费 | a级国产| 欧美成人xxx | 欧美一级毛片免费高清aa | 在线观看久草 | 特黄视频| 亚洲天堂爱爱 | 亚洲久久网站 | 特色毛片| 亚洲国产三级 | 欧美成年免费a级 | 高清国产一区二区三区 | 爽爽爽爽爽爽爽成人免费观看 | 波多野结衣中文无毒不卡 | 在线免费黄色网址 |