javascript
SpringBoot: SpringBoot里面创建导出Excel的接口(亲测)
在Web項目中,難免需要導(dǎo)出Excel這樣的功能,后端接口怎么實現(xiàn)呢,Controller代碼在下面,復(fù)制到項目的Controller中即可使用:
首先加入Excel的依賴,本例中我們用apache的poi:
<dependency>
? ? <groupId>org.apache.poi</groupId>
? ? <artifactId>poi</artifactId>
? ? <version>3.17</version>
</dependency>
1
2
3
4
5
后臺導(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對象
? ? ?* @throws IOException 拋IO異常
? ? ?*/
? ? @RequestMapping("/ExcelDownload")
? ? public void excelDownload(HttpServletResponse response) throws IOException {
? ? ? ? //表頭數(shù)據(jù)
? ? ? ? String[] header = {"ID", "姓名", "性別", "年齡", "地址", "分數(shù)"};
? ? ? ? //數(shù)據(jù)內(nèi)容
? ? ? ? String[] student1 = {"1", "小紅", "女", "23", "成都青羊區(qū)", "96"};
? ? ? ? String[] student2 = {"2", "小強", "男", "26", "成都金牛區(qū)", "91"};
? ? ? ? String[] student3 = {"3", "小明", "男", "28", "成都武侯區(qū)", "90"};
? ? ? ? //聲明一個工作簿
? ? ? ? HSSFWorkbook workbook = new HSSFWorkbook();
? ? ? ? //生成一個表格,設(shè)置表格名稱為"學(xué)生表"
? ? ? ? HSSFSheet sheet = workbook.createSheet("學(xué)生表");
? ? ? ? //設(shè)置表格列寬度為10個字節(jié)
? ? ? ? sheet.setDefaultColumnWidth(10);
? ? ? ? //創(chuàng)建第一行表頭
? ? ? ? HSSFRow headrow = sheet.createRow(0);
? ? ? ? //遍歷添加表頭(下面模擬遍歷學(xué)生,也是同樣的操作過程)
? ? ? ? for (int i = 0; i < header.length; i++) {
? ? ? ? ? ? //創(chuàng)建一個單元格
? ? ? ? ? ? HSSFCell cell = headrow.createCell(i);
? ? ? ? ? ? //創(chuàng)建一個內(nèi)容對象
? ? ? ? ? ? HSSFRichTextString text = new HSSFRichTextString(header[i]);
? ? ? ? ? ? //將內(nèi)容對象的文字內(nèi)容寫入到單元格中
? ? ? ? ? ? cell.setCellValue(text);
? ? ? ? }
? ? ? ? //模擬遍歷結(jié)果集,把內(nèi)容加入表格
? ? ? ? //模擬遍歷第一個學(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);
? ? ? ? }
? ? ? ? //模擬遍歷第二個學(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);
? ? ? ? }
? ? ? ? //模擬遍歷第三個學(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);
? ? ? ? }
? ? ? ? //準備將Excel的輸出流通過response輸出到頁面下載
? ? ? ? //八進制輸出流
? ? ? ? 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());
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
然后訪問接口,彈出頁面:
下載該Excel,打開后如下圖:
至此為止,SpringBoot的后臺Controller接口導(dǎo)出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表格導(dǎo)出
? ? ?* @param response HttpServletResponse對象
? ? ?* @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 {
? ? ? ? //聲明一個工作簿
? ? ? ? HSSFWorkbook workbook = new HSSFWorkbook();
? ? ? ? //生成一個表格,設(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)建一個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)建一個內(nèi)容對象
? ? ? ? ? ? ? ? HSSFRichTextString text = new HSSFRichTextString(data.get(i));
? ? ? ? ? ? ? ? //將內(nèi)容對象的文字內(nèi)容寫入到單元格中
? ? ? ? ? ? ? ? cell.setCellValue(text);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //準備將Excel的輸出流通過response輸出到頁面下載
? ? ? ? //八進制輸出流
? ? ? ? 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();
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
以上方法調(diào)用示例:
/**
?* Excel表格導(dǎo)出接口
?* 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);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
本文參考文章(感謝原作者):https://blog.csdn.net/panpan96/article/details/76566475
————————————————
版權(quán)聲明:本文為CSDN博主「李先森LeeCode」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_39448458/article/details/83013677
總結(jié)
以上是生活随笔為你收集整理的SpringBoot: SpringBoot里面创建导出Excel的接口(亲测)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 滴滴6年亏390亿是真的吗 表示创业6年
- 下一篇: Servlet基础之HttpServle