-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExcelUtils.java
More file actions
328 lines (294 loc) · 10.1 KB
/
ExcelUtils.java
File metadata and controls
328 lines (294 loc) · 10.1 KB
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
package com.just.athena.dispatch.common.util;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class ExcelUtils {
/**
* 导出excl
*
* @param culomnNames
* @param culomnBodys
* @param outputStream
* @param sheetSize
*/
public static void doExport(List<String> culomnNames,
List<Map<Integer, String>> culomnBodys, OutputStream outputStream,
int[] sheetSize) {
HSSFWorkbook workbook = new HSSFWorkbook();
int sheetPage = 1;
if ((null == culomnBodys) || (culomnBodys.size() <= 0)
|| (null == sheetSize) || (sheetSize.length <= 0)
|| (sheetSize[0] == 0)) {
sheetSize = new int[]{culomnBodys.size()};
}
if (sheetSize[0] != 0) {
sheetPage = culomnBodys.size() / sheetSize[0];
}
for (int m = 0; m < sheetPage; m++) {
HSSFSheet sheet = workbook.createSheet();
sheet.setDefaultColumnWidth((short) 15);
HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor((short) 40);
style.setFillPattern((short) 1);
style.setBorderBottom((short) 1);
style.setBorderLeft((short) 1);
style.setBorderRight((short) 1);
style.setBorderTop((short) 1);
style.setAlignment((short) 2);
HSSFFont font = workbook.createFont();
font.setColor((short) 20);
font.setFontHeightInPoints((short) 12);
font.setBoldweight((short) 700);
style.setFont(font);
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor((short) 43);
style2.setFillPattern((short) 1);
style2.setBorderBottom((short) 1);
style2.setBorderLeft((short) 1);
style2.setBorderRight((short) 1);
style2.setBorderTop((short) 1);
style2.setAlignment((short) 2);
style2.setVerticalAlignment((short) 1);
HSSFFont font2 = workbook.createFont();
font2.setBoldweight((short) 400);
style2.setFont(font2);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(
0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
comment.setAuthor("leno");
HSSFRow row = sheet.createRow(0);
for (short i = 0; i < culomnNames.size(); i = (short) (i + 1)) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(
(String) culomnNames.get(i));
cell.setCellValue(text);
}
int pageSize = 0;
if (sheetSize[0] != 0) {
pageSize = m == sheetPage ? culomnBodys.size() % sheetSize[0]
: sheetSize[0];
}
for (int i = 0; i < pageSize; i++) {
row = sheet.createRow(i + 1);
Map targetMap = (Map) culomnBodys.get(m * sheetSize[0] + i);
for (int j = 0; j < targetMap.size(); j++) {
HSSFCell cell = row.createCell(j);
cell.setCellStyle(style2);
String value = (String) targetMap.get(Integer.valueOf(j));
HSSFRichTextString text = new HSSFRichTextString(value);
cell.setCellValue(text);
}
}
}
try {
workbook.write(outputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 得到Excel
*
* @param filePath
* @return
* @throws IOException
* @throws InvalidFormatException
*/
public static Workbook createWorkbook(String filePath)
throws InvalidFormatException, IOException {
Workbook workbook = WorkbookFactory.create(new File(filePath));
return workbook;
}
/**
* 通过Workbook得到所有sheet名字
*
* @param workbook
* @return
*/
public static List<String> getAllSheetName(Workbook workbook) {
List<String> sheetNameList = new ArrayList<String>();
int numberOfSheets = workbook.getNumberOfSheets();
for (int i = 0; i < numberOfSheets; i++) {
Sheet sheetAt = workbook.getSheetAt(i);
String sheetName = sheetAt.getSheetName();
sheetNameList.add(sheetName);
}
return sheetNameList;
}
/**
* 以字符串返回单元格数据
*
* @param cell
* @return
*/
public static String getCellValue(Cell cell) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String result = "";
int cellType = cell.getCellType();
DecimalFormat df = new DecimalFormat("#.00");// 使用DecimalFormat类对科学计数法格式的数字进行格式化
switch (cellType) {
// 字符串
case HSSFCell.CELL_TYPE_STRING:
result = cell.getStringCellValue();
break;
// 实数
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
result = sdf.format(cell.getDateCellValue());
} else {
result = df.format(cell.getNumericCellValue());
}
break;
// 公式
case HSSFCell.CELL_TYPE_FORMULA:
result = String.valueOf(cell.getNumericCellValue());
break;
// 布尔
case HSSFCell.CELL_TYPE_BOOLEAN:
result = String.valueOf(cell.getBooleanCellValue());
break;
// 空白
case HSSFCell.CELL_TYPE_BLANK:
result = "";
break;
// 错误
case HSSFCell.CELL_TYPE_ERROR:
result = "";
break;
default:
System.out.println("枚举了所有类型");
break;
}
return result;
}
public static void export(OutputStream os, String title, String[] headers,
List<Map<String, Object>> excelMapList, String[] columnFields) {
try {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
int index = 0;
// ****************创建表格标题行*******************/
Row titleRow = sheet.createRow(index++);
// 标题行样式
CellStyle titleStyle = wb.createCellStyle();
// 标题行字体
Font titleFont = wb.createFont();
titleFont.setFontName("黑体");
titleFont.setFontHeightInPoints((short) 16);// 设置字体大小
titleStyle.setFont(titleFont);
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
// 标题表高度
titleRow.setHeight((short) (20 * 20));
Cell titleCell = titleRow.createCell(0);
// 为标题行列设置样式
titleCell.setCellStyle(titleStyle);
titleCell.setCellValue(title);
// 合并行
CellRangeAddress region = new CellRangeAddress(0, 0, (short) 0,
headers.length - 1);
sheet.addMergedRegion(region);
// ****************创建表格表头*******************/
// 表头样式
// 背景
CellStyle headerStyle = wb.createCellStyle();
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT
.getIndex());
headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
Row headerRow = sheet.createRow(index++);
// 表头高度
headerRow.setHeight((short) (16 * 20));
for (int i = 0; i < headers.length; i++) {
Cell headerCell = headerRow.createCell(i);
headerCell.setCellStyle(headerStyle);
headerCell.setCellValue(headers[i]);
if (i == 0) {
sheet.setColumnWidth(i, 10 * 256);
} else {
sheet.setColumnWidth(i, 20 * 256);
}
}
// ****************创建内容*******************/
// 内容样式
CellStyle contentStyle = wb.createCellStyle();
contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
for (int i = 0; i < excelMapList.size(); i++) {
Row contentRow = sheet.createRow(i + index);
// 内容行高度
contentRow.setHeight((short) (16 * 20));
Map<String, Object> record = excelMapList.get(i);
for (int j = 0; j < columnFields.length; j++) {
Cell contentCell = contentRow.createCell(j);
contentCell.setCellStyle(contentStyle);
Object object = record.get(columnFields[j]);
if (object != null) {
if (object instanceof Double) {
contentCell.setCellValue(Double.parseDouble(object
.toString()));
} else if (object instanceof Integer) {
contentCell.setCellValue(Integer.parseInt(object
.toString()));
} else if (object instanceof Date) {
// Date date =
// DateUtil.convertStringToDate(object.toString(),DateUtil.mm);
// contentCell.setCellValue(DateUtil.convertDateToString(date,
// DateUtil.mm));
} else {
contentCell.setCellValue(object.toString() + " ");
}
} else {
contentCell.setCellValue("");
}
}
}
wb.write(os);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void exportToExcel(HttpServletResponse response,
String fileName, String title, String[] headers,
List<Map<String, Object>> excelMapList, String[] columnFields) {
ServletOutputStream sos = null;
try {
String name = new String((fileName + ".xls").getBytes("UTF-8"),
"iso8859-1");
response.setHeader("Content-Disposition", "attachment;filename="
+ name + "");// 指定下载的文件名
response.setContentType("applicationnd.ms-excel;charset=UTF-8");
sos = response.getOutputStream();
ExcelUtils.export(sos, title, headers, excelMapList, columnFields);
sos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sos != null)
try {
sos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}