分享一个基于 POI 和反射机制实现的通用的写出 Excel 文档文件工具类,及简写出数据到 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package org.fanlychie.excel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.fanlychie.util.DynaBean;
/**
* 可进行写操作的 EXCEL 工具类
*
* @author fanlychie
*/
public class WritableExcel {
// 数据列表
private List<?> list;
// 属性名称集
private String[] props;
// 标题集
private String[] title;
// 动态 Bean
private DynaBean dynaBean;
// 工作表
private XSSFSheet sheet;
// 工作表的名称
private String sheetName;
// 工作簿
private XSSFWorkbook workbook;
// 数据格式
private static Map<Class<?>, String> dataFormat;
/**
* 构造一个可写的 Excel 实例
*
* @param list
* 数据列表
* @param mapper
* 数据对象的属性名称与Excel文档标题之间的映射关系
*/
public WritableExcel(List<?> list, LinkedHashMap<String, String> mapper) {
init(list, mapper);
}
/**
* 写出到文件
*
* @param file
* 文件对象
*/
public boolean write(File file) {
try {
return write(new FileOutputStream(file));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
/**
* 写出到输出流
*
* @param out
* 输出流
*/
public boolean write(OutputStream out) {
try {
// 创建工作表
sheet = workbook.createSheet(sheetName);
// 构建并填充标题行
fillTitleRow();
// 主体行的索引
int rowIndex = 1;
for (Object bean : list) {
// 创建并填充行
fillCreatedRow(rowIndex++, bean);
}
workbook.write(out);
return true;
} catch (Throwable e) {
throw new RuntimeException(e);
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {}
}
}
/**
* 设置工作表的名称, 默认名称 Sheet1
*
* @param sheetName
* 工作表的名称
*/
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
/**
* 设置日期格式, 默认格式 yyyy-MM-dd
*
* @param dateFormat
* 日期格式
*/
public void setDateFormat(String dateFormat) {
dataFormat.put(Date.class, dateFormat);
}
/**
* 初始化
*
* @param list
* 数据列表
* @param mapper
* 数据对象的属性名称与Excel文档标题之间的映射关系
*/
private void init(List<?> list, LinkedHashMap<String, String> mapper) {
this.list = list;
int index = 0;
int size = mapper.size();
props = new String[size];
title = new String[size];
// 迭代取出属性名称集和标题集
for (String prop : mapper.keySet()) {
props[index] = prop;
title[index] = mapper.get(prop);
index++;
}
// 工作簿
this.workbook = new XSSFWorkbook();
// 动态 Bean
this.dynaBean = new DynaBean(list.get(0).getClass());
// 默认的工作表名称
this.sheetName = "Sheet1";
}
/**
* 构建并填充标题行
*
* @throws Throwable
*/
private void fillTitleRow() throws Throwable {
// 单元格索引
int cellIndex = 0;
// 单元格宽度
int cellWidth = 18 * 256;
// 创建一行
XSSFRow row = sheet.createRow(0);
// 设置行高
row.setHeightInPoints(25);
// 标题行样式
CellStyle style = getTitleCellStyle();
// 迭代标题集
for (String text : title) {
// 设置宽度
sheet.setColumnWidth(cellIndex, cellWidth);
// 创建单元格
XSSFCell cell = row.createCell(cellIndex++);
// 设置单元格样式
cell.setCellStyle(style);
// 设置单元格的值
cell.setCellValue(text);
}
}
/**
* 创建并填充行
*
* @param index
* 行的索引
* @param bean
* 填充行的对象
* @throws Throwable
*/
private void fillCreatedRow(int index, Object bean) throws Throwable {
// 单元格索引
int cellIndex = 0;
// 创建一行
XSSFRow row = sheet.createRow(index);
// 设置行高
row.setHeightInPoints(20);
// 迭代 Bean 属性集
for (String prop : props) {
// 属性的值
Object value = dynaBean.getFieldValue(bean, prop);
// 属性类型
Class<?> type = dynaBean.getFieldType(prop);
// 创建单元格
XSSFCell cell = row.createCell(cellIndex++);
// 值为空
if (value == null) {
// 设置为空串
cell.setCellValue("");
// 单元格样式
cell.setCellStyle(getBodyCellStyle(String.class));
} else {
// 按类型设值
if (type == Boolean.TYPE || type == Boolean.class) {
boolean boolValue = Boolean.parseBoolean(value.toString());
cell.setCellValue(boolValue);
} else if ((Number.class.isAssignableFrom(type) || type.isPrimitive())
&& type != Byte.TYPE && type != Character.TYPE) {
double doubleValue = Double.parseDouble(value.toString());
cell.setCellValue(doubleValue);
} else if (type == Date.class) {
Date dateValue = (Date) value;
cell.setCellValue(dateValue);
} else {
cell.setCellValue(value.toString());
}
// 单元格样式
cell.setCellStyle(getBodyCellStyle(type));
}
}
}
/**
* 主体单元格样式
*
* @param type
* 单元格填充的数据的类型
* @return CellStyle
*/
private CellStyle getBodyCellStyle(Class<?> type) {
// 单元格样式
CellStyle style = workbook.createCellStyle();
// 单元格数据格式
String cellDataFormat = dataFormat.get(type);
if (cellDataFormat == null) {
cellDataFormat = dataFormat.get(String.class);
}
// 水平居中
style.setAlignment(CellStyle.ALIGN_CENTER);
// 垂直居中
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
// 背景颜色
setBackgroundColor(style, IndexedColors.LIGHT_TURQUOISE.index);
// 字体
style.setFont(getFont(11, IndexedColors.GREY_50_PERCENT.index));
// 数据格式
style.setDataFormat(workbook.createDataFormat().getFormat(cellDataFormat));
// 自动换行
style.setWrapText(true);
return style;
}
/**
* 标题行样式
*
* @return CellStyle
*/
private CellStyle getTitleCellStyle() {
// 单元格样式
CellStyle style = workbook.createCellStyle();
// 水平居中
style.setAlignment(CellStyle.ALIGN_CENTER);
// 垂直居中
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
// 背景颜色
setBackgroundColor(style, IndexedColors.YELLOW.index);
// 字体
style.setFont(getFont(12, IndexedColors.BLUE_GREY.index));
// 数据格式
style.setDataFormat(workbook.createDataFormat().getFormat("GENERAL"));
// 自动换行
style.setWrapText(true);
return style;
}
/**
* 设置单元格背景颜色
*
* @param style
* 单元格样式
* @param color
* 颜色值
*/
private void setBackgroundColor(CellStyle style, short color) {
// 边框设置
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setBorderTop(CellStyle.BORDER_THIN);
// 边框颜色
style.setBottomBorderColor(IndexedColors.GREY_25_PERCENT.index);
style.setLeftBorderColor(IndexedColors.GREY_25_PERCENT.index);
style.setRightBorderColor(IndexedColors.GREY_25_PERCENT.index);
style.setTopBorderColor(IndexedColors.GREY_25_PERCENT.index);
// 背景颜色
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(color);
}
/**
* 获取字体
*
* @param fontSize
* 字体大小
* @param fontColor
* 字体颜色
* @return Font
*/
private Font getFont(int fontSize, short fontColor) {
// 创建字体
Font font = workbook.createFont();
// 字体颜色
font.setColor(fontColor);
// 字体大小
font.setFontHeightInPoints((short) fontSize);
// 字体名称
if (System.getProperty("os.name").contains("Windows")) {
font.setFontName("Microsoft YaHei");
}
return font;
}
// 数据格式
static {
dataFormat = new HashMap<Class<?>, String>();
dataFormat.put(Short.TYPE, "0");
dataFormat.put(Short.class, "0");
dataFormat.put(Integer.TYPE, "0");
dataFormat.put(Integer.class, "0");
dataFormat.put(Long.TYPE, "0");
dataFormat.put(Long.class, "0");
dataFormat.put(Float.TYPE, "0.00");
dataFormat.put(Float.class, "0.00");
dataFormat.put(Double.TYPE, "0.00");
dataFormat.put(Double.class, "0.00");
dataFormat.put(String.class, "GENERAL");
dataFormat.put(Date.class, "yyyy-MM-dd");
}
}

POI 依赖

1
2
3
4
5
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>

测试对象清单

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
public static class Person {
private int id;
private String name;
private boolean local;
private Date birthday;
public Person() {
}
public Person(int id, String name, boolean local, Date birthday) {
this.id = id;
this.name = name;
this.local = local;
this.birthday = birthday;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isLocal() {
return local;
}
public void setLocal(boolean local) {
this.local = local;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}

测试清单1

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
private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args) throws Throwable {
File excelFile = new File("src/test/resources/test_excel.xlsx");
List<Person> list = new ArrayList<Person>();
for (int i = 1; i < 10; i++) {
list.add(new Person(i, "范忠云" + i, i % 2 == 0, format.parse("1990-04-0" + i)));
}
// 属性与 Excel 列的映射关系
LinkedHashMap<String, String> mapper = new LinkedHashMap<String, String>();
mapper.put("id", "编号");
mapper.put("name", "姓名");
mapper.put("local", "本地户口");
mapper.put("birthday", "出生日期");
// 创建一个可写的 Excel 对象
WritableExcel excel = new WritableExcel(list, mapper);
// 写出 Excel 文档文件
boolean success = excel.write(excelFile);
if (success) {
System.out.println("Excel 生成成功!");
}
}

测试结果