|
|
1
|
+package com.tianbo.util.POI;
|
|
|
2
|
+
|
|
|
3
|
+
|
|
|
4
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
5
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
6
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
7
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
8
|
+
|
|
|
9
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
10
|
+import java.io.*;
|
|
|
11
|
+import java.lang.reflect.Field;
|
|
|
12
|
+import java.lang.reflect.Method;
|
|
|
13
|
+import java.util.Iterator;
|
|
|
14
|
+import java.util.List;
|
|
|
15
|
+
|
|
|
16
|
+/**
|
|
|
17
|
+ * @author: lucifer
|
|
|
18
|
+ * @description: Excel导出工具类
|
|
|
19
|
+ */
|
|
|
20
|
+@Slf4j
|
|
|
21
|
+public class ExportExcelUtil<T> {
|
|
|
22
|
+ /**
|
|
|
23
|
+ * excel ./xls后缀
|
|
|
24
|
+ */
|
|
|
25
|
+ public static final String EXTENSION_XLS = ".xls";
|
|
|
26
|
+
|
|
|
27
|
+
|
|
|
28
|
+ /**
|
|
|
29
|
+ * excel ./xlsx后缀
|
|
|
30
|
+ */
|
|
|
31
|
+ public static final String EXTENSION_XLSX = ".xlsx";
|
|
|
32
|
+
|
|
|
33
|
+ /**
|
|
|
34
|
+ * 导出文件夹路径
|
|
|
35
|
+ */
|
|
|
36
|
+ public static final String EXCEL_PATH = "./upload/";
|
|
|
37
|
+
|
|
|
38
|
+
|
|
|
39
|
+
|
|
|
40
|
+ /**
|
|
|
41
|
+ * 兼容.xls和.xlsx格式
|
|
|
42
|
+ *
|
|
|
43
|
+ * @param originalFilename
|
|
|
44
|
+ * @return
|
|
|
45
|
+ */
|
|
|
46
|
+ private Workbook getWorkbook(String originalFilename) {
|
|
|
47
|
+ Workbook workbook = null;
|
|
|
48
|
+ if (originalFilename.endsWith(EXTENSION_XLS)) {
|
|
|
49
|
+ workbook = new HSSFWorkbook();
|
|
|
50
|
+ } else if (originalFilename.endsWith(EXTENSION_XLSX)) {
|
|
|
51
|
+ workbook = new XSSFWorkbook();
|
|
|
52
|
+ }
|
|
|
53
|
+ return workbook;
|
|
|
54
|
+ }
|
|
|
55
|
+
|
|
|
56
|
+
|
|
|
57
|
+ /**
|
|
|
58
|
+ * 导出
|
|
|
59
|
+ *
|
|
|
60
|
+ * @param originalFilename
|
|
|
61
|
+ * @param sheetName
|
|
|
62
|
+ * @param headers
|
|
|
63
|
+ * @param columns
|
|
|
64
|
+ * @param lists
|
|
|
65
|
+ * @throws Exception
|
|
|
66
|
+ */
|
|
|
67
|
+ public void export(String originalFilename, String sheetName, String[] headers, String[] columns, List<T> lists,HttpServletResponse response) throws Exception {
|
|
|
68
|
+ Workbook workbook = getWorkbook(originalFilename);
|
|
|
69
|
+ Sheet sheet = workbook.createSheet(sheetName);
|
|
|
70
|
+ sheet.setDefaultColumnWidth(15);
|
|
|
71
|
+ //设置表头样式
|
|
|
72
|
+ CellStyle style = setHeaderStyle(workbook);
|
|
|
73
|
+ Row row = sheet.createRow(0);
|
|
|
74
|
+ for (int i = 0; i < headers.length; i++) {
|
|
|
75
|
+ Cell headerCell = row.createCell(i);
|
|
|
76
|
+ headerCell.setCellValue(headers[i]);
|
|
|
77
|
+ headerCell.setCellStyle(style);
|
|
|
78
|
+ }
|
|
|
79
|
+ Iterator<T> it = lists.iterator();
|
|
|
80
|
+ int rowIndex = 0;
|
|
|
81
|
+ while (it.hasNext()) {
|
|
|
82
|
+ rowIndex++;
|
|
|
83
|
+ row = sheet.createRow(rowIndex);
|
|
|
84
|
+ T t = it.next();
|
|
|
85
|
+ Field[] fields = t.getClass()
|
|
|
86
|
+ .getDeclaredFields();
|
|
|
87
|
+ for (int i = 0; i < fields.length; i++) {
|
|
|
88
|
+ Field field = fields[i];
|
|
|
89
|
+ String fieldName = field.getName();
|
|
|
90
|
+ for (int j = 0; j < columns.length; j++) {
|
|
|
91
|
+ if (fieldName.equals(columns[j])) {
|
|
|
92
|
+ String getMethodName = "get" +
|
|
|
93
|
+ fieldName.substring(0, 1).
|
|
|
94
|
+ toUpperCase() + fieldName.
|
|
|
95
|
+ substring(1);
|
|
|
96
|
+ Class cls = t.getClass();
|
|
|
97
|
+ Method getMethod = cls.getMethod(
|
|
|
98
|
+ getMethodName, new Class[]{});
|
|
|
99
|
+ Object val = getMethod.invoke(
|
|
|
100
|
+ t, new Object[]{});
|
|
|
101
|
+ String textVal;
|
|
|
102
|
+ if (null != val) {
|
|
|
103
|
+ textVal = val.toString();
|
|
|
104
|
+ } else {
|
|
|
105
|
+ textVal = "";
|
|
|
106
|
+ }
|
|
|
107
|
+ Cell cell = row.createCell(j);
|
|
|
108
|
+ cell.setCellValue(textVal);
|
|
|
109
|
+// CellStyle cellStyle = setCellStyle(workbook, i);
|
|
|
110
|
+ cell.setCellStyle(style);
|
|
|
111
|
+ //单元格列宽设置
|
|
|
112
|
+ sheet.autoSizeColumn(j);
|
|
|
113
|
+ sheet.setColumnWidth(j, sheet.getColumnWidth(j)*17/10 > 255 * 256 ? 255 * 256 : sheet.getColumnWidth(j)*17/10);
|
|
|
114
|
+
|
|
|
115
|
+ log.info("i:===============" + i + ",j:================" + j + ",textVal:" + textVal);
|
|
|
116
|
+ }
|
|
|
117
|
+ }
|
|
|
118
|
+ }
|
|
|
119
|
+ }
|
|
|
120
|
+ String filename = sheetName + System.currentTimeMillis() + ".xls";
|
|
|
121
|
+ //判断是否有文件夹,没有就创建
|
|
|
122
|
+ File file = new File(EXCEL_PATH);
|
|
|
123
|
+ if (!file.exists()) {
|
|
|
124
|
+ file.mkdirs();
|
|
|
125
|
+ }
|
|
|
126
|
+ String filepath = EXCEL_PATH + filename;
|
|
|
127
|
+
|
|
|
128
|
+
|
|
|
129
|
+ System.out.println("filepath:" + filepath);
|
|
|
130
|
+ FileOutputStream out = new FileOutputStream(filepath);
|
|
|
131
|
+ workbook.write(out);
|
|
|
132
|
+ out.close();
|
|
|
133
|
+
|
|
|
134
|
+ downloadExcel(filepath, response);
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
|
137
|
+ /**
|
|
|
138
|
+ * 设置表头样式
|
|
|
139
|
+ *
|
|
|
140
|
+ * @param workbook
|
|
|
141
|
+ * @return
|
|
|
142
|
+ */
|
|
|
143
|
+ private CellStyle setHeaderStyle(Workbook workbook) {
|
|
|
144
|
+ CellStyle cellStyle = workbook.createCellStyle();
|
|
|
145
|
+ //水平居中
|
|
|
146
|
+ cellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION);
|
|
|
147
|
+ //垂直居中
|
|
|
148
|
+ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
149
|
+ //设置边框
|
|
|
150
|
+ cellStyle.setBorderTop(BorderStyle.THIN);
|
|
|
151
|
+ cellStyle.setBorderRight(BorderStyle.THIN);
|
|
|
152
|
+ cellStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
153
|
+ cellStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
154
|
+ //设置字体
|
|
|
155
|
+ Font cellFont = workbook.createFont();
|
|
|
156
|
+ cellFont.setBold(true);
|
|
|
157
|
+ cellStyle.setFont(cellFont);
|
|
|
158
|
+ return cellStyle;
|
|
|
159
|
+ }
|
|
|
160
|
+
|
|
|
161
|
+ /**
|
|
|
162
|
+ * 设置单元格样式
|
|
|
163
|
+ */
|
|
|
164
|
+ private CellStyle setCellStyle(Workbook workbook, int i) {
|
|
|
165
|
+ CellStyle cellStyle = workbook.createCellStyle();
|
|
|
166
|
+ //奇数列 左对齐
|
|
|
167
|
+ if ((i & 1) != 1) {
|
|
|
168
|
+ cellStyle.setAlignment(HorizontalAlignment.LEFT);
|
|
|
169
|
+ } else {
|
|
|
170
|
+ //水平居中
|
|
|
171
|
+ cellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION);
|
|
|
172
|
+ }
|
|
|
173
|
+ //垂直居中
|
|
|
174
|
+ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
175
|
+ //设置边框
|
|
|
176
|
+ cellStyle.setBorderTop(BorderStyle.THIN);
|
|
|
177
|
+ cellStyle.setBorderRight(BorderStyle.THIN);
|
|
|
178
|
+ cellStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
179
|
+ cellStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
180
|
+ //设置字体
|
|
|
181
|
+ Font cellFont = workbook.createFont();
|
|
|
182
|
+ cellFont.setFontName("仿宋_GB2312");
|
|
|
183
|
+ cellStyle.setFont(cellFont);
|
|
|
184
|
+ return cellStyle;
|
|
|
185
|
+ }
|
|
|
186
|
+
|
|
|
187
|
+
|
|
|
188
|
+ /**
|
|
|
189
|
+ * 下载
|
|
|
190
|
+ */
|
|
|
191
|
+ public static void downloadExcel(String filepath, HttpServletResponse response)
|
|
|
192
|
+ throws IOException {
|
|
|
193
|
+ File file = new File(filepath);
|
|
|
194
|
+ String fileName = file.getName();
|
|
|
195
|
+ response.setContentType("application/vnd.ms-excel;charset=utf-8");
|
|
|
196
|
+ response.addHeader("Content-Transfer-Encoding", "binary");
|
|
|
197
|
+ response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1"));
|
|
|
198
|
+ response.setCharacterEncoding("utf-8");
|
|
|
199
|
+ InputStream fis = new BufferedInputStream(new FileInputStream(file));
|
|
|
200
|
+ byte[] b = new byte[fis.available()];
|
|
|
201
|
+ fis.read(b);
|
|
|
202
|
+ response.getOutputStream().write(b);
|
|
|
203
|
+ fis.close();
|
|
|
204
|
+ log.info("导出完毕");
|
|
|
205
|
+ }
|
|
|
206
|
+
|
|
|
207
|
+}
|
|
|
208
|
+
|
|
|
209
|
+ |