在許多項目中,將數據庫資料導出為 Excel 是一個很常見的需求,具體實現簡單來說就是后端提供導出接口,前端下載導出數據即可。Jakarta POI 是一套用於訪問微軟格式文檔的 Java API,它由很多組件組成,其中有用於操作 Excel 格式文件的 HSSF 和用於操作 Word 的 HWPF,本文使用前者實現 Excel 文件的導出。
(2)前端代碼比較簡單,只要在用戶點擊“導出”按鈕時,執行如下代碼發起請求下載文件即可:
(3)下載下來的 Excel 里的內容如下:
一、將數據導出成 excel 文件
1,添加依賴
編輯項目的 pom.xml 文件,添加 poi 相關依賴:
1
2
3
4
5
|
<
dependency
>
<
groupId
>org.apache.poi</
groupId
>
<
artifactId
>poi</
artifactId
>
<
version
>3.17</
version
>
</
dependency
>
|
2,創建數據模型
這里我們定義一個 User 模型對象,后面將會將其導出成 excel。
1
2
3
4
5
6
7
8
9
10
|
@Getter
@Setter
@AllArgsConstructor
public
class
User {
private
int
id;
private
String name;
private
String gender;
private
Date birthday;
private
String workID;
}
|
3,創建導出工具類
為方便使用,這里封裝一個用於導出數據的工具類。里面主要工作是構建一個 HSSFWorkbook 並進行一些 Excel 基本信息配置(文檔信息、列的寬度、表頭配置等),然后遍歷 users 集合,將數據導出到 Excel 中。
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
|
public
class
PoiUtils {
// 將數據導出成excel文件
public
static
ResponseEntity<
byte
[]> exportUser2Excel(List<User> users) {
HttpHeaders headers =
null
;
ByteArrayOutputStream baos =
null
;
try
{
//1.創建Excel文檔
HSSFWorkbook workbook =
new
HSSFWorkbook();
//2.創建文檔摘要
workbook.createInformationProperties();
//3.獲取文檔信息,並配置
DocumentSummaryInformation dsi = workbook.getDocumentSummaryInformation();
//3.1文檔類別
dsi.setCategory(
"人員信息"
);
//3.2設置文檔管理員
dsi.setManager(
"hangge"
);
//3.3設置組織機構
dsi.setCompany(
"航歌"
);
//4.獲取摘要信息並配置
SummaryInformation si = workbook.getSummaryInformation();
//4.1設置文檔主題
si.setSubject(
"人員信息表"
);
//4.2.設置文檔標題
si.setTitle(
"人員信息"
);
//4.3 設置文檔作者
si.setAuthor(
"hangge"
);
//4.4設置文檔備注
si.setComments(
"備注信息暫無"
);
//創建Excel表單
HSSFSheet sheet = workbook.createSheet(
"2019年人員信息"
);
//創建日期顯示格式
HSSFCellStyle dateCellStyle = workbook.createCellStyle();
dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(
"m/d/yy"
));
//創建標題的顯示樣式
HSSFCellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//定義列的寬度
sheet.setColumnWidth(
0
,
5
*
256
);
sheet.setColumnWidth(
1
,
12
*
256
);
sheet.setColumnWidth(
2
,
10
*
256
);
sheet.setColumnWidth(
3
,
5
*
256
);
sheet.setColumnWidth(
4
,
12
*
256
);
//5.設置表頭
HSSFRow headerRow = sheet.createRow(
0
);
HSSFCell cell0 = headerRow.createCell(
0
);
cell0.setCellValue(
"編號"
);
cell0.setCellStyle(headerStyle);
HSSFCell cell1 = headerRow.createCell(
1
);
cell1.setCellValue(
"姓名"
);
cell1.setCellStyle(headerStyle);
HSSFCell cell2 = headerRow.createCell(
2
);
cell2.setCellValue(
"工號"
);
cell2.setCellStyle(headerStyle);
HSSFCell cell3 = headerRow.createCell(
3
);
cell3.setCellValue(
"性別"
);
cell3.setCellStyle(headerStyle);
HSSFCell cell4 = headerRow.createCell(
4
);
cell4.setCellValue(
"出生日期"
);
cell4.setCellStyle(headerStyle);
//6.裝數據
for
(
int
i =
0
; i < users.size(); i++) {
HSSFRow row = sheet.createRow(i +
1
);
User user = users.get(i);
row.createCell(
0
).setCellValue(user.getId());
row.createCell(
1
).setCellValue(user.getName());
row.createCell(
2
).setCellValue(user.getWorkID());
row.createCell(
3
).setCellValue(user.getGender());
HSSFCell birthdayCell = row.createCell(
4
);
birthdayCell.setCellValue(user.getBirthday());
birthdayCell.setCellStyle(dateCellStyle);
}
headers =
new
HttpHeaders();
headers.setContentDispositionFormData(
"attachment"
,
new
String(
"人員信息表.xls"
.getBytes(
"UTF-8"
),
"iso-8859-1"
));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
baos =
new
ByteArrayOutputStream();
workbook.write(baos);
}
catch
(IOException e) {
e.printStackTrace();
}
return
new
ResponseEntity<
byte
[]>(baos.toByteArray(), headers, HttpStatus.CREATED);
}
}
|
4,使用樣例
(1)我們再創建一個 Controller 調用這個工具類來導出 excel 文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@RestController
public
class
HelloController {
@GetMapping
(
"/exportUser"
)
public
ResponseEntity<
byte
[]> exportUser() {
// 准備需要導出的數據
List<User> users =
new
ArrayList<>();
users.add(
new
User(
1
,
"張三"
,
"男"
,
new
Date(),
"01001"
));
users.add(
new
User(
2
,
"李四"
,
"男"
,
new
Date(),
"01002"
));
users.add(
new
User(
3
,
"王五"
,
"女"
,
new
Date(),
"01003"
));
// 將數據導出成excel
return
PoiUtils.exportUser2Excel(users);
}
}
|
(2)前端代碼比較簡單,只要在用戶點擊“導出”按鈕時,執行如下代碼發起請求下載文件即可:
1
|
window.open(
"/exportUser"
,
"parent"
);
|
(3)下載下來的 Excel 里的內容如下:
原文出自:www.hangge.com 轉載請保留原文鏈接:https://www.hangge.com/blog/cache/detail_2721.html