Java-报表导出

原生Java 导出

基础

Apache POI 是用Java 编写的免费开源的跨平台的Java API ,Apache POI 提供API 给Java 程序对Microsoft Office(Excel、WORD、PowerPoint、Visio 等)格式档案读和写的功能。
POI 为”Poor Obfuscation Implementation” 的首字母缩写,意为“可怜的模糊实现”。

实现

https://github.com/vgbhfive/SpringBootDemo -> poidemo

主要代码

ExcelUtil:

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package cn.vgbhfive.poidemo.utils;

import org.apache.poi.hssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* Excel 工具类
*
* @time: 2019/10/13
* @author: Vgbh
*/
@Service
public class ExcelUtils {

private static final Logger logger = LoggerFactory.getLogger(ExcelUtils.class);

private static final short borderRpx = 1;

/**
* 导出Excel
* @param headers
* @param data
* @return
*/
public static HSSFWorkbook expExcel(List<String> headers, List<List<String>> data) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("sheet1");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = null;
HSSFCellStyle style = workbook.createCellStyle();
setBorderStyle(style, borderRpx);
style.setFont(setFontStyle(workbook, "黑体", (short) 14));
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
sheet.createFreezePane(0, 1, 0, 1);

for (int i = 0; i < headers.size(); i++) {
cell = row.createCell(i);
cell.setCellValue(headers.get(i));
cell.setCellStyle(style);
}

HSSFCellStyle style1 = workbook.createCellStyle();
setBorderStyle(style1, borderRpx);
style1.setFont(setFontStyle(workbook, "宋体", (short) 12));
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
for (int i = 0; i < data.size(); i++) {
row = sheet.createRow(i+1);
List<String> param = data.get(i);
for (int j = 0; j < param.size(); j++) {
cell = row.createCell(j);
cell.setCellValue(param.get(j));
cell.setCellStyle(style1);
}
}

for (int i = 0; i < headers.size(); i++) {
sheet.autoSizeColumn(i);
}

return workbook;
}

/**
* 导出文件
* @param workbook
* @param path
* @param response
*/
public static void outFile(HSSFWorkbook workbook, String path, HttpServletResponse response) {
OutputStream os=null;
File file = null;
try {
file = new File(path);
String filename = file.getName();
os = new FileOutputStream(file);
response.addHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(filename, "UTF-8"));
os= new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/vnd.ms-excel;charset=utf-8");
workbook.write(os);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.flush();
os.close();
System.gc();
System.out.println(file.delete());
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 设置字体样式
* @param workbook
* @param name
* @param height
* @return
*/
public static HSSFFont setFontStyle(HSSFWorkbook workbook, String name, short height) {
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints(height);
font.setFontName(name);
return font;
}

/**
* 设置单元格样式
* @param cellStyle
* @param border
*/
public static void setBorderStyle(HSSFCellStyle cellStyle, short border) {
cellStyle.setBorderBottom(border); // 下边框
cellStyle.setBorderLeft(border);// 左边框
cellStyle.setBorderTop(border);// 上边框
cellStyle.setBorderRight(border);// 右边框
}

}

展示

1.jpg


Spring Boot 组件导出

doing…


引用


个人备注

此博客内容均为作者学习所做笔记,侵删!
若转作其他用途,请注明来源!