|
@@ -0,0 +1,271 @@
|
|
1
|
+package com.xhkjedu.sexam.utils;
|
|
2
|
+
|
|
3
|
+import com.xhkjedu.sexam.model.paperstudent.EPsqbatchDetail;
|
|
4
|
+import com.xhkjedu.sexam.vo.paperstudent.EPsqbatchStuVo;
|
|
5
|
+import com.xhkjedu.utils.N_Utils;
|
|
6
|
+import lombok.extern.slf4j.Slf4j;
|
|
7
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
8
|
+import org.apache.poi.ss.usermodel.*;
|
|
9
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
10
|
+import org.springframework.stereotype.Component;
|
|
11
|
+import org.springframework.web.multipart.MultipartFile;
|
|
12
|
+
|
|
13
|
+import java.io.InputStream;
|
|
14
|
+import java.text.DecimalFormat;
|
|
15
|
+import java.text.SimpleDateFormat;
|
|
16
|
+import java.util.*;
|
|
17
|
+import java.util.stream.Collectors;
|
|
18
|
+
|
|
19
|
+/**
|
|
20
|
+ * @author ywx
|
|
21
|
+ * @classname PoiUtils
|
|
22
|
+ * @description
|
|
23
|
+ * @date 2022-11-21 14:46:22
|
|
24
|
+ **/
|
|
25
|
+@Component
|
|
26
|
+@Slf4j
|
|
27
|
+public class PoiUtils {
|
|
28
|
+ //2003- 版本的excel
|
|
29
|
+ private final static String excel2003L = ".xls";
|
|
30
|
+ //2007+ 版本的excel
|
|
31
|
+ private final static String excel2007U = ".xlsx";
|
|
32
|
+
|
|
33
|
+ /**
|
|
34
|
+ * 导入教师
|
|
35
|
+ *
|
|
36
|
+ * @return java.util.Map<java.lang.String, java.lang.Object>
|
|
37
|
+ * @Param [file]
|
|
38
|
+ * @Author ywx
|
|
39
|
+ * @Date 2022/3/17 9:44
|
|
40
|
+ **/
|
|
41
|
+ public static Map<String, Object> readExcelRecord(MultipartFile file, List<Map> qns, List<Map> students) throws Exception {
|
|
42
|
+ Map<String, Object> resultMap = new HashMap<>();
|
|
43
|
+ Map<Integer, List<String>> errMsg = new LinkedHashMap<>();
|
|
44
|
+ try {
|
|
45
|
+ // 数据校验
|
|
46
|
+ isExcelEmpty(file);
|
|
47
|
+ InputStream in = file.getInputStream();
|
|
48
|
+ String fileName = file.getOriginalFilename();
|
|
49
|
+ Workbook work = getWorkbook(in, fileName);
|
|
50
|
+ Sheet sheet;// 分表数据
|
|
51
|
+ Row row;// 行数据
|
|
52
|
+ sheet = work.getSheetAt(0);//只取第一个sheet
|
|
53
|
+ if (sheet == null) {
|
|
54
|
+ throw new Exception("Excel第一个分表内容为空!");
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ int rows = sheet.getLastRowNum(); // 获取最后一个实际行的下标,比行数小1
|
|
58
|
+ int firstRowNum = sheet.getFirstRowNum(); // 获取第一个实际行的下标,(firstRowNum=0)
|
|
59
|
+ row = sheet.getRow(firstRowNum);//标题行
|
|
60
|
+ int firstCellNum = row.getFirstCellNum();
|
|
61
|
+ int lastCellNum = row.getLastCellNum();// 每一行的最后一列,获取列数,比最后一列列标大1
|
|
62
|
+ String qn = qns.stream().map(q -> q.get("qn").toString()).collect(Collectors.joining());
|
|
63
|
+ Map<Integer, Integer> questionMap = new HashMap<>();
|
|
64
|
+ Map<Integer, Double> scoreMap = new HashMap<>();
|
|
65
|
+ for (int i = 0; i < qns.size(); i++) {
|
|
66
|
+ questionMap.put(i + 2, (Integer) qns.get(i).get("eptqid"));
|
|
67
|
+ scoreMap.put(i + 2, (Double) qns.get(i).get("score"));
|
|
68
|
+ }
|
|
69
|
+ String titleName = "姓名账号" + qn;//标题格式
|
|
70
|
+ StringBuilder sb = new StringBuilder();
|
|
71
|
+ for (int y = firstCellNum; y < lastCellNum; y++) {
|
|
72
|
+ sb.append(row.getCell(y));
|
|
73
|
+ }
|
|
74
|
+ int maxIndex = lastCellNum - 1;//最大题号下标
|
|
75
|
+ String importTitleName = sb.toString();
|
|
76
|
+ if (!titleName.equals(importTitleName)) throw new Exception("标题格式不正确");
|
|
77
|
+
|
|
78
|
+ Map<String, Integer> stuMap = students.stream().collect(Collectors.toMap(
|
|
79
|
+ s -> s.get("username").toString() + s.get("loginname").toString()
|
|
80
|
+ , s -> (Integer) s.get("studentid")));
|
|
81
|
+
|
|
82
|
+ // 导入记录信息
|
|
83
|
+ List<EPsqbatchStuVo> stus = new ArrayList<>();
|
|
84
|
+
|
|
85
|
+ //遍历当前sheet中的所有行 排除第一行 第一行为标题头 不纳入遍历
|
|
86
|
+ for (int x = firstRowNum + 1; x <= rows; x++) {
|
|
87
|
+ // 获取行
|
|
88
|
+ row = sheet.getRow(x);
|
|
89
|
+ if (row == null) {//若此行为空,则跳出循环继续下一行
|
|
90
|
+ continue;
|
|
91
|
+ } else if (isEmptyCell(row.getCell(0)) && isEmptyCell(row.getCell(1))) {
|
|
92
|
+ continue;
|
|
93
|
+ }
|
|
94
|
+ firstCellNum = row.getFirstCellNum();// 每一行的第一列,(firstCellNum=0)
|
|
95
|
+ if (firstCellNum == 1) throw new Exception("第" + (x + 1) + "行姓名不能为空");
|
|
96
|
+ lastCellNum = row.getLastCellNum();// 每一行的最后一列,获取列数,比最后一列列标大1
|
|
97
|
+ List<String> mrows = new ArrayList<>();
|
|
98
|
+ //遍历所有的列
|
|
99
|
+ String username = getCellValue(row.getCell(0));//姓名
|
|
100
|
+ String loginname = getCellValue(row.getCell(1));//账号
|
|
101
|
+ Integer studentid = stuMap.get(username + loginname);
|
|
102
|
+ if (N_Utils.isEmptyInteger(studentid)) {
|
|
103
|
+ mrows.add("第" + (x + 1) + "行学生【" + username + "(" + loginname + ")】不存在");
|
|
104
|
+ continue;//学生不存在处理下一行
|
|
105
|
+ }
|
|
106
|
+ List<EPsqbatchDetail> details = new ArrayList<>();
|
|
107
|
+ for (int y = firstCellNum + 2; y < lastCellNum; y++) {
|
|
108
|
+ Cell cell = row.getCell(y);
|
|
109
|
+ String score = getCellValue(cell);
|
|
110
|
+ if (N_Utils.isEmpty(score)) {
|
|
111
|
+ continue;//学生分值为空处理下一列
|
|
112
|
+ }
|
|
113
|
+ EPsqbatchDetail detail = new EPsqbatchDetail();
|
|
114
|
+ detail.setStudentid(studentid);
|
|
115
|
+ Double stuscore = Double.valueOf(score);
|
|
116
|
+ Double qscore = scoreMap.get(y);
|
|
117
|
+ if (Double.compare(stuscore, qscore) == 1) {
|
|
118
|
+ stuscore = qscore;
|
|
119
|
+ }
|
|
120
|
+ detail.setNewscore(stuscore);
|
|
121
|
+ detail.setEptqid(questionMap.get(y));
|
|
122
|
+ details.add(detail);
|
|
123
|
+ if (y == maxIndex && N_Utils.isListNotEmpty(mrows)) {
|
|
124
|
+ errMsg.put(x + 1, mrows);
|
|
125
|
+ }
|
|
126
|
+ }
|
|
127
|
+ if (N_Utils.isListNotEmpty(details)) {
|
|
128
|
+ EPsqbatchStuVo stu = new EPsqbatchStuVo();
|
|
129
|
+ stu.setStudentid(studentid);
|
|
130
|
+ stu.setDetails(details);
|
|
131
|
+ stus.add(stu);
|
|
132
|
+ }
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ resultMap.put("code", 0);
|
|
136
|
+ resultMap.put("stus", stus);
|
|
137
|
+ } catch (Exception e) {
|
|
138
|
+ resultMap.put("code", 1);
|
|
139
|
+ String msg = e.getMessage();
|
|
140
|
+ resultMap.put("msg", msg);
|
|
141
|
+ if (msg.contains("Invalid ")) {
|
|
142
|
+ msg = "格式不正确";
|
|
143
|
+ }
|
|
144
|
+ throw new Exception(msg);
|
|
145
|
+ }
|
|
146
|
+ resultMap.put("errMsg", errMsg);
|
|
147
|
+ return resultMap;
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ /**
|
|
151
|
+ * 根据文件后缀,自适应上传文件的版本
|
|
152
|
+ *
|
|
153
|
+ * @return org.apache.poi.ss.usermodel.Workbook
|
|
154
|
+ * @Param [inStr, fileName]
|
|
155
|
+ * @Author ywx
|
|
156
|
+ * @Date 2020/6/3 16:42
|
|
157
|
+ **/
|
|
158
|
+ public static Workbook getWorkbook(InputStream inStr, String fileName) throws Exception {
|
|
159
|
+ Workbook wb;
|
|
160
|
+ String fileType = fileName.substring(fileName.lastIndexOf("."));
|
|
161
|
+ if (excel2003L.equals(fileType)) {
|
|
162
|
+ wb = new HSSFWorkbook(inStr); //2003-
|
|
163
|
+ } else if (excel2007U.equals(fileType)) {
|
|
164
|
+ wb = new XSSFWorkbook(inStr); //2007+
|
|
165
|
+ } else {
|
|
166
|
+ throw new Exception("Excel格式解析有误!");
|
|
167
|
+ }
|
|
168
|
+ return wb;
|
|
169
|
+ }
|
|
170
|
+
|
|
171
|
+ /**
|
|
172
|
+ * 判断单元格是否是空值
|
|
173
|
+ *
|
|
174
|
+ * @return java.lang.Boolean
|
|
175
|
+ * @Param [cell]
|
|
176
|
+ * @Author ywx
|
|
177
|
+ * @Date 2020/6/3 16:37
|
|
178
|
+ **/
|
|
179
|
+ public static Boolean isEmptyCell(Cell cell) {
|
|
180
|
+ if (cell != null) {
|
|
181
|
+ Boolean rtn = true;
|
|
182
|
+ if (N_Utils.isNotEmpty(PoiUtils.getCellValue(cell))) {
|
|
183
|
+ rtn = false;
|
|
184
|
+ }
|
|
185
|
+ return rtn;
|
|
186
|
+ } else {
|
|
187
|
+ return true;
|
|
188
|
+ }
|
|
189
|
+ }
|
|
190
|
+
|
|
191
|
+ /**
|
|
192
|
+ * 对表格中数值进行格式化
|
|
193
|
+ *
|
|
194
|
+ * @return java.lang.Object
|
|
195
|
+ * @Param [cell]
|
|
196
|
+ * @Author ywx
|
|
197
|
+ * @Date 2020/6/3 16:41
|
|
198
|
+ **/
|
|
199
|
+ public static String getCellValue(Cell cell) {
|
|
200
|
+ String value = null;
|
|
201
|
+ DecimalFormat df = new DecimalFormat("0"); //格式化number String字符
|
|
202
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //日期格式化
|
|
203
|
+ DecimalFormat df2 = new DecimalFormat("0"); //格式化数字
|
|
204
|
+ switch (cell.getCellTypeEnum()) {
|
|
205
|
+ case STRING:
|
|
206
|
+ value = cell.getRichStringCellValue().getString();
|
|
207
|
+ break;
|
|
208
|
+ case NUMERIC:
|
|
209
|
+ String cellStyle = cell.getCellStyle().getDataFormatString();
|
|
210
|
+ if ("General".equals(cellStyle)) {
|
|
211
|
+ Double d = cell.getNumericCellValue();
|
|
212
|
+ if (d.toString().contains("E")) {
|
|
213
|
+ cell.setCellType(CellType.STRING);
|
|
214
|
+ value = cell.getStringCellValue();
|
|
215
|
+ } else {
|
|
216
|
+ value = df.format(d);
|
|
217
|
+ }
|
|
218
|
+ } else if ("m/d/yy".equals(cellStyle)) {
|
|
219
|
+ value = sdf.format(cell.getDateCellValue());
|
|
220
|
+ } else {
|
|
221
|
+ value = df2.format(cell.getNumericCellValue());
|
|
222
|
+ }
|
|
223
|
+ break;
|
|
224
|
+ case BOOLEAN:
|
|
225
|
+ boolean rtn = cell.getBooleanCellValue();
|
|
226
|
+ if (rtn) {
|
|
227
|
+ value = "1";//真
|
|
228
|
+ } else {
|
|
229
|
+ value = "0";//假
|
|
230
|
+ }
|
|
231
|
+ break;
|
|
232
|
+ case BLANK:
|
|
233
|
+ value = "";
|
|
234
|
+ break;
|
|
235
|
+ case FORMULA://读取excel公式的值
|
|
236
|
+ try {
|
|
237
|
+ value = String.valueOf(cell.getNumericCellValue());
|
|
238
|
+ } catch (IllegalStateException e) {
|
|
239
|
+ value = String.valueOf(cell.getRichStringCellValue());
|
|
240
|
+ }
|
|
241
|
+ break;
|
|
242
|
+ default:
|
|
243
|
+ break;
|
|
244
|
+ }
|
|
245
|
+ return N_Utils.strTrim(value);
|
|
246
|
+ }
|
|
247
|
+
|
|
248
|
+ /**
|
|
249
|
+ * 检验Excel内容是否为空
|
|
250
|
+ *
|
|
251
|
+ * @return void
|
|
252
|
+ * @Param [file]
|
|
253
|
+ * @Author ywx
|
|
254
|
+ * @Date 2020/6/3 16:36
|
|
255
|
+ **/
|
|
256
|
+ public static void isExcelEmpty(MultipartFile file) throws Exception {
|
|
257
|
+ if (null == file || file.isEmpty()) {
|
|
258
|
+ throw new Exception("文件不存在!");
|
|
259
|
+ }
|
|
260
|
+ InputStream in = file.getInputStream();
|
|
261
|
+ String fileName = file.getOriginalFilename();
|
|
262
|
+ Workbook work = getWorkbook(in, fileName);
|
|
263
|
+ if (null == work) {
|
|
264
|
+ throw new Exception("Excel内容为空!");
|
|
265
|
+ }
|
|
266
|
+ Integer sheet_size = work.getNumberOfSheets();
|
|
267
|
+ if (sheet_size == 0) {
|
|
268
|
+ throw new Exception("Excel内容为空!");
|
|
269
|
+ }
|
|
270
|
+ }
|
|
271
|
+}
|