ExcelService.java
7.5 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
package com.skua.modules.excel.service;
import com.skua.core.api.vo.Result;
import com.skua.modules.excel.callback.ICellCallback;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.*;
/**
* <pre>
* Excel 导入/导出/下载模板
* </pre>
*
* @author sonin
* @version 1.0 2022/4/27 10:17
*/
public abstract class ExcelService {
/**
* <pre>
* 导入操作
* </pre>
*
* @param request
* @author sonin
* @Description: TODO(这里描述这个方法的需求变更情况)
*/
public abstract Result importHandle(HttpServletRequest request) throws Exception;
/**
* <pre>
* 导出操作
* </pre>
*
* @param request
* @author sonin
* @Description: TODO(这里描述这个方法的需求变更情况)
*/
public abstract Workbook exportHandle(HttpServletRequest request) throws Exception;
/**
* <pre>
* 获取子类的excelMap
* </pre>
* @param
* @author sonin
* @Description: TODO(这里描述这个方法的需求变更情况)
*/
protected abstract Map<String, String> getExcelMap();
/**
* <pre>
* 导出模板操作
* </pre>
*
* @param request
* @author sonin
* @Description: TODO(这里描述这个方法的需求变更情况)
*/
public Workbook templateHandle(HttpServletRequest request) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
this.initTitle(workbook, new ArrayList<>(getExcelMap().keySet()));
return workbook;
}
/**
* <pre>
* 创建时,初始化标题行
* </pre>
*
* @param workbook
* @param titleList
* @author sonin
* @Description: TODO(这里描述这个方法的需求变更情况)
*/
protected void initTitle(HSSFWorkbook workbook, List<String> titleList) {
// 创建sheet0
HSSFSheet sheet0 = workbook.createSheet("sheet0");
// 设置 居中样式
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 创建 第0行 => 标题行
HSSFRow row0 = sheet0.createRow(0);
HSSFCell curCell;
for (int i = 0; i < titleList.size(); i++) {
curCell = row0.createCell(i);
curCell.setCellValue(titleList.get(i));
curCell.setCellStyle(cellStyle);
}
}
/**
* <pre>
* 导出时填充POI
* </pre>
*
* @param workbook
* @param fieldNameList
* @param dataList
* @author sonin
* @Description: TODO(这里描述这个方法的需求变更情况)
*/
protected <T> void fillExcel(HSSFWorkbook workbook, Collection<String> fieldNameList, List<T> dataList, ICellCallback iCellCallback) throws Exception {
HSSFSheet sheet0 = workbook.getSheetAt(0);
HSSFRow curRow;
HSSFCell curCell;
Field field;
int i = 0, j;
Object srcFieldVal, targetFieldVal;
for (T data : dataList) {
// 每一条数据创建一行
curRow = sheet0.createRow(i + 1);
j = 0;
for (String fieldName : fieldNameList) {
curCell = curRow.createCell(j);
if (data instanceof Map) {
srcFieldVal = ((Map) data).get(fieldName);
} else {
field = data.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
srcFieldVal = field.get(data);
field.setAccessible(false);
}
if (iCellCallback != null) {
targetFieldVal = iCellCallback.doCellConvert(fieldName, srcFieldVal);
} else {
targetFieldVal = srcFieldVal;
}
if (targetFieldVal != null) {
curCell.setCellValue("" + targetFieldVal);
}
j++;
}
i++;
}
}
/**
* <pre>
* 解析导入的文件
* </pre>
*
* @param request
* @author sonin
* @Description: TODO(这里描述这个方法的需求变更情况)
*/
protected List<Map<String, Object>> analysisExcel(HttpServletRequest request, List<String> fieldNameList) throws Exception {
MultiValueMap<String, MultipartFile> multiValueMap = ((StandardMultipartHttpServletRequest) request).getMultiFileMap();
List<Map<String, Object>> dataList = new ArrayList<>();
Map<String, Object> data;
List<MultipartFile> fileList;
Workbook workbook;
Sheet sheet0;
Row curRow;
Cell curCell;
for (Map.Entry<String, List<MultipartFile>> item : multiValueMap.entrySet()) {
// String name = item.getKey(); // 请求的文本参数中key值
fileList = item.getValue();
// 请求的文本参数列表
for (MultipartFile multipartFile : fileList) {
workbook = WorkbookFactory.create(multipartFile.getInputStream());
// 默认只有一个sheet
sheet0 = workbook.getSheetAt(0);
// 遍历sheet有多少行;并跳过第0行,此行是标题行
for (int i = 1; i < sheet0.getPhysicalNumberOfRows(); i++) {
curRow = sheet0.getRow(i);
if (curRow == null) {
continue;
}
data = new HashMap<>();
dataList.add(data);
for (int j = 0; j < curRow.getLastCellNum(); j++) {
curCell = curRow.getCell(j);
if (curCell == null) {
continue;
}
switch (curCell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
// 如果是日期类型
if (HSSFDateUtil.isCellDateFormatted(curCell)) {
data.put(fieldNameList.get(j), curCell.getDateCellValue());
} else {
data.put(fieldNameList.get(j), curCell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_STRING:
data.put(fieldNameList.get(j), curCell.getStringCellValue());
break;
case Cell.CELL_TYPE_BLANK:
data.put(fieldNameList.get(j), "");
break;
case Cell.CELL_TYPE_BOOLEAN:
data.put(fieldNameList.get(j), curCell.getBooleanCellValue());
break;
default:
curCell.setCellType(Cell.CELL_TYPE_STRING);
data.put(fieldNameList.get(j), curCell.getStringCellValue());
break;
}
}
}
workbook.cloneSheet(0);
}
}
return dataList;
}
}