Original blog address: https://blog.csdn.net/yuan487639/article/details/76212170
This function involves two knowledge points, one is to use excel-related plug-ins, and the other is to export files. How to configure spring-mvc
Let's talk about the use of the excel plug-in first. This time, the org.apache.poi package is used, and the version is 3.9. The code that can run is pasted below.
The entity class of the plug-in can also be understood as a parameter. The build design mode is adopted here. The advantage is that some default values are initialized, and the code is convenient to write when using it. For table export, I think three parameters are enough, title, header and data, so, these three parameter construction methods I designed must be passed, others use default parameters
package com.ucredit.util;
import org.apache.poi.hssf.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
public class ExcelUtil {
private ExcelUtil() {
}
public static void export(ExcelParam excelParam, HttpServletResponse response) throws IOException {
if (excelParam.widths == null) {
excelParam.widths = new int[excelParam.headers.length];
for (int i = 0; i < excelParam.headers.length; i++) {
excelParam.widths[i] = excelParam.width;
}
}
if (excelParam.ds_format == null) {
excelParam.ds_format = new int[excelParam.headers.length];
for (int i = 0; i < excelParam.headers.length; i++) {
excelParam.ds_format[i] = 1;
}
}
//创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
//创建一个sheet
HSSFSheet sheet = wb.createSheet("excel");
int rowCount = 0;
if (excelParam.headers != null) {
HSSFRow row = sheet.createRow(rowCount);
//表头样式
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
for (int i = 0; i < excelParam.headers.length; i++) {
sheet.setColumnWidth(i, excelParam.widths[i]);
HSSFCell cell = row.createCell(i);
cell.setCellValue(excelParam.headers[i]);
cell.setCellStyle(style);
}
rowCount++;
}
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//表格主体 解析list
for (int i = 0; i < excelParam.data.size(); i++) { //行数
HSSFRow row = sheet.createRow(rowCount);
for (int j = 0; j < excelParam.headers.length; j++) { //列数
HSSFCell cell = row.createCell(j);
cell.setCellValue(excelParam.data.get(i)[j]);
cell.setCellStyle(style);
}
rowCount++;
}
//设置文件名
String fileName = excelParam.name + ".xls";
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setHeader("Pragma", "No-cache");
OutputStream outputStream = response.getOutputStream();
wb.write(outputStream);
outputStream.flush();
outputStream.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
In fact, it can be seen from this method that the final export of the file is actually writing the file to the outputStream in the response, and the subsequent work is done by the browser.