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
| package com.colalife.core.util.excel;
public class ExcelExportUtil {
private static Logger logger = LoggerFactory.getLogger(ExcelExportUtil.class);
private static Map<String, CellStyle> styles;
/**
* sheetName Excel导出文档里的标签页名,默认为Sheet1
*/
public static final String DEFAULT_SHEET_NAME = "Sheet1";
/**
* excel导出时文件名,默认为"导出excel.xls"
*/
public static final String DEFAULT_EXCEL_FILE_NAME = "导出excel.xls";
/**
* Excel导出,sheetName默认为Sheet1
* @param dataList 数据,可用bean list
* @param columns 字段和字段显示名的键值对
*/
public static void export(List<?> dataList, Map<String, String> columns, HttpServletResponse response)
throws IOException {
String[] fields = columns.keySet().toArray(new String[0]);
String[] titles = columns.values().toArray(new String[0]);
export(dataList, fields, titles, response);
}
/**
* Excel导出,sheetName默认为Sheet1
* @param dataList 数据,可用bean list
* @param EasyuiDataGrid 用来获取fields titles 字段和字段显示名的数组
*/
public static void export(List<?> dataList, EasyuiDataGrid dg, HttpServletResponse response)
throws IOException {
String[] fields = dg.getFields().split(",");
String[] titles = dg.getTitles().split(",");
Workbook excel = ExcelExportUtil.getExcel(dataList, fields, titles);
ExcelExportUtil.setResponseForExcel(response);
excel.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
}
/**
* Excel导出,sheetName默认为Sheet1
* @param dataList 数据,可用bean list
* @param fields titles 字段和字段显示名的数组
*/
public static void export(List<?> dataList, String[] fields, String[] titles, HttpServletResponse response)
throws IOException {
Workbook excel = ExcelExportUtil.getExcel(dataList, fields, titles);
ExcelExportUtil.setResponseForExcel(response);
excel.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
}
/**
* Excel导出的实现,sheetName默认为Sheet1
* @param data 数据,可用bean list
* @param columns 字段和字段显示名的键值对
*/
public static Workbook getExcel(List<?> data, Map<String, String> columns) {
return getExcel(data, DEFAULT_SHEET_NAME, columns);
}
/**
* Excel导出的实现,sheetName默认为Sheet1
* @param data 数据,可用bean list
* @param columns 字段和字段显示名的键值对
*/
public static Workbook getExcel(List<?> data, String[] fields, String[] titles) {
return getExcel(data, DEFAULT_SHEET_NAME, fields, titles);
}
/**
* Excel导出的实现
* @param data 数据,可用bean list
* @param sheetName 导出文档里的标签页名
* @param columns 字段和字段显示名的键值对
*/
public static Workbook getExcel(List<?> data, String sheetName, Map<String, String> columns) {
String[] fields = columns.keySet().toArray(new String[0]);
String[] titles = columns.values().toArray(new String[0]);
return getExcel(data, sheetName, fields, titles);
}
/**
* Excel导出的实现
* @param data 数据,可用bean list
* @param sheetName 导出文档里的标签页名
* @param fields titles 字段和字段显示名的数组
*/
public static Workbook getExcel(List<?> data, String sheetName, String[] fields, String[] titles) {
Workbook workbook = new HSSFWorkbook();
//创建所有Cell Style
createStyles(workbook);
Sheet sheet = workbook.createSheet(sheetName);
// 创建第一行,为excel表头
Row row = sheet.createRow(0);
// 序号列
Cell numHeader = row.createCell(0);
numHeader.setCellValue(new HSSFRichTextString("序号"));
numHeader.setCellStyle(styles.get("header"));
for (int i = 1; i <= titles.length; i++) {
Cell header = row.createCell(i);
header.setCellValue(new HSSFRichTextString(titles[i - 1]));
header.setCellStyle(styles.get("header"));
}
// 创建其他各数据行
if (!data.isEmpty()) {
for (int j = 0; j < data.size(); j++) {
Object bean = data.get(j);
Row dataRow = sheet.createRow(j + 1);
// 序号列
Cell num = dataRow.createCell(0);
num.setCellValue(new HSSFRichTextString(String.valueOf(j + 1)));
num.setCellStyle(styles.get("normal"));
for (int k = 1; k <= fields.length; k++) {
Cell cell = dataRow.createCell(k);
Object value = null;
boolean isDate = false;
try {
value = PropertyUtils.getSimpleProperty(bean, fields[k - 1]);
if (value instanceof Date) {
value = DateUtil.formatDate((Date) value);
isDate = true;
}
} catch (Exception e) {
logger.error("Excel导出,获取属性值异常:" + e.getMessage());
}
HSSFRichTextString cellValue = new HSSFRichTextString(ObjectUtils.toString(value));
cell.setCellValue(cellValue);
cell.setCellStyle(isDate ? styles.get("dateCell") : styles.get("normal"));
}
}
}
for (int i = 0; i < titles.length; i++) {
// 自适应宽度
sheet.autoSizeColumn(i);
}
return workbook;
}
/**
* excel导出时用到的方法,直接调用
*/
public static void setResponseForExcel(HttpServletResponse response) {
setResponseForExcel(response, DEFAULT_EXCEL_FILE_NAME);
}
/**
* excel导出时用到的方法,直接调用
* @throws UnsupportedEncodingException
*/
public static void setResponseForExcel(HttpServletResponse response, String fileName) {
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
try {
// 解决excel文件乱码问题
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("GB2312"), "ISO8859-1"));
} catch (UnsupportedEncodingException e) {
logger.error("Excel导出,转码异常:" + e.getMessage());
}
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setDateHeader("Expires", 0);
}
private static Map<String, CellStyle> createStyles(Workbook wb) {
styles = Maps.newHashMap();
DataFormat df = wb.createDataFormat();
// --字体设定 --//
//普通字体
Font normalFont = wb.createFont();
normalFont.setFontName("宋体");
normalFont.setFontHeightInPoints((short) 12);
//加粗字体
Font boldFont = wb.createFont();
boldFont.setFontHeightInPoints((short) 12);
boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
//蓝色加粗字体
Font blueBoldFont = wb.createFont();
blueBoldFont.setFontHeightInPoints((short) 12);
blueBoldFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
blueBoldFont.setColor(IndexedColors.BLUE.getIndex());
// --Cell Style设定-- //
//普通格式
CellStyle normalStyle = wb.createCellStyle();
normalStyle.setFont(normalFont);
styles.put("normal", normalStyle);
//标题格式
CellStyle headerStyle = wb.createCellStyle();
headerStyle.setFont(boldFont);
// 指定单元格居中对齐
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 指定单元格垂直居中对齐
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
styles.put("header", headerStyle);
//日期格式
CellStyle dateCellStyle = wb.createCellStyle();
dateCellStyle.setFont(normalFont);
dateCellStyle.setDataFormat(df.getFormat("yyyy-MM-dd HH:mm:ss"));
// setBorder(dateCellStyle);
styles.put("dateCell", dateCellStyle);
//数字格式
CellStyle numberCellStyle = wb.createCellStyle();
numberCellStyle.setFont(normalFont);
numberCellStyle.setDataFormat(df.getFormat("#,##0.00"));
setBorder(numberCellStyle);
styles.put("numberCell", numberCellStyle);
//合计列格式
CellStyle totalStyle = wb.createCellStyle();
totalStyle.setFont(blueBoldFont);
totalStyle.setWrapText(true);
totalStyle.setAlignment(CellStyle.ALIGN_RIGHT);
setBorder(totalStyle);
styles.put("total", totalStyle);
return styles;
}
private static void setBorder(CellStyle style) {
//设置边框
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
}
}
|