|
@@ -0,0 +1,303 @@
|
|
1
|
+package com.xhkjedu.smarking.utils;
|
|
2
|
+
|
|
3
|
+import com.xhkjedu.smarking.model.stupaper.MsPaperStudentScore;
|
|
4
|
+import com.xhkjedu.smarking.vo.exam.ESubjectVo;
|
|
5
|
+import com.xhkjedu.smarking.vo.stupaper.PaperStuScoreVo;
|
|
6
|
+import com.xhkjedu.utils.N_Utils;
|
|
7
|
+import lombok.extern.slf4j.Slf4j;
|
|
8
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
9
|
+import org.apache.poi.ss.usermodel.*;
|
|
10
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
11
|
+import org.springframework.stereotype.Component;
|
|
12
|
+import org.springframework.web.multipart.MultipartFile;
|
|
13
|
+
|
|
14
|
+import java.io.InputStream;
|
|
15
|
+import java.text.DecimalFormat;
|
|
16
|
+import java.text.SimpleDateFormat;
|
|
17
|
+import java.util.ArrayList;
|
|
18
|
+import java.util.HashMap;
|
|
19
|
+import java.util.List;
|
|
20
|
+import java.util.Map;
|
|
21
|
+
|
|
22
|
+/*
|
|
23
|
+ * @Description:导入
|
|
24
|
+ * @Author: WN
|
|
25
|
+ * @Date: 2024/11/13 11:31:52
|
|
26
|
+ **/
|
|
27
|
+@Component
|
|
28
|
+@Slf4j
|
|
29
|
+public class ImportUtil {
|
|
30
|
+ //2003- 版本的excel
|
|
31
|
+ private final static String excel2003L = ".xls";
|
|
32
|
+ //2007+ 版本的excel
|
|
33
|
+ private final static String excel2007U = ".xlsx";
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+ public static List<MsPaperStudentScore> readExcelStudentScore(MultipartFile file, Map<String,ESubjectVo> subjectMap,Map<String,PaperStuScoreVo> stuMap,List<String> classNames,Integer createid) throws Exception {
|
|
37
|
+ List<MsPaperStudentScore> rtnList = new ArrayList<>();//需要保存的学生分数
|
|
38
|
+ Integer timestamp = N_Utils.getSecondTimestamp();
|
|
39
|
+
|
|
40
|
+ try {
|
|
41
|
+ // 数据校验
|
|
42
|
+ isExcelEmpty(file);
|
|
43
|
+ InputStream in = file.getInputStream();
|
|
44
|
+ String fileName = file.getOriginalFilename();
|
|
45
|
+ Workbook work = getWorkbook(in, fileName);
|
|
46
|
+ //遍历所有sheet
|
|
47
|
+ for (int i = 0; i < work.getNumberOfSheets(); i++) {
|
|
48
|
+ Sheet sheet = work.getSheetAt(i);
|
|
49
|
+ int rows = sheet.getLastRowNum(); // 获取最后一个实际行的下标,比行数小1
|
|
50
|
+ String sheetName = sheet.getSheetName();//根据sheet名称获取科目信息
|
|
51
|
+ //获取科目信息
|
|
52
|
+ if(!subjectMap.containsKey(sheetName)) throw new Exception("科目信息不存在");
|
|
53
|
+
|
|
54
|
+ ESubjectVo subject = subjectMap.get(sheetName);//sheet对应科目信息
|
|
55
|
+
|
|
56
|
+ int firstRowNum = sheet.getFirstRowNum(); // 获取第一个实际行的下标,(firstRowNum=0)
|
|
57
|
+ Row row = sheet.getRow(firstRowNum);//标题行
|
|
58
|
+ String titleName = "姓名性别班级准考证号补录成绩";//标题格式
|
|
59
|
+ StringBuilder sb = new StringBuilder();
|
|
60
|
+ sb.append(row.getCell(0));//姓名
|
|
61
|
+ sb.append(row.getCell(1));//性别
|
|
62
|
+ sb.append(row.getCell(2));//班级
|
|
63
|
+ sb.append(row.getCell(3));//准考证号
|
|
64
|
+ sb.append(row.getCell(4));//补录成绩
|
|
65
|
+ String importTitleName = sb.toString().replaceAll(" |(必填)", "");
|
|
66
|
+ if(!titleName.equals(importTitleName)) throw new Exception("标题格式不正确");
|
|
67
|
+
|
|
68
|
+ Map<String, String> nameMap = new HashMap<>();
|
|
69
|
+
|
|
70
|
+ /*
|
|
71
|
+ * 遍历当前sheet中的所有行
|
|
72
|
+ * 排除第一行 第一行为标题头 不纳入遍历
|
|
73
|
+ */
|
|
74
|
+ for (int x = firstRowNum + 1; x <= rows; x++) {
|
|
75
|
+ // 获取行
|
|
76
|
+ row = sheet.getRow(x);
|
|
77
|
+ if (row == null) {//若此行为空,则跳出循环继续下一行
|
|
78
|
+ continue;
|
|
79
|
+ } else if (isEmptyCell(row.getCell(0)) && isEmptyCell(row.getCell(1))) {
|
|
80
|
+ continue;
|
|
81
|
+ }
|
|
82
|
+ PaperStuScoreVo stu = new PaperStuScoreVo();// 每一行一个学生信息
|
|
83
|
+ int firstCellNum = row.getFirstCellNum();// 每一行的第一列,(firstCellNum=0)
|
|
84
|
+ if (firstCellNum == 1) throw new Exception("第" + (x + 1) + "行姓名不能为空");
|
|
85
|
+ int lastCellNum = row.getLastCellNum();// 每一行的最后一列,获取列数,比最后一列列标大1
|
|
86
|
+ //遍历所有的列
|
|
87
|
+ for (int y = firstCellNum; y < lastCellNum; y++) {
|
|
88
|
+ Cell cell = row.getCell(y);
|
|
89
|
+ if (y == 0) { // 姓名
|
|
90
|
+ if (isEmptyCell(cell)) {
|
|
91
|
+ throw new Exception("第" + (x + 1) + "行姓名不能为空");
|
|
92
|
+ } else {
|
|
93
|
+ stu.setStudentname(getCellValue(cell));
|
|
94
|
+ }
|
|
95
|
+ } else if (y == 1) {//性别
|
|
96
|
+ if (isEmptyCell(cell)) {
|
|
97
|
+ stu.setUsersex(0);
|
|
98
|
+ } else {
|
|
99
|
+ String usersex = getCellValue(cell);
|
|
100
|
+ if (usersex.equals("女")) {
|
|
101
|
+ stu.setUsersex(2);
|
|
102
|
+ } else if (usersex.equals("男")) {
|
|
103
|
+ stu.setUsersex(1);
|
|
104
|
+ } else {
|
|
105
|
+ stu.setUsersex(0);
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+ } else if (y == 2) {//班级
|
|
109
|
+ if (isEmptyCell(cell)) {
|
|
110
|
+ throw new Exception("第" + (x + 1) + "行班级不能为空");
|
|
111
|
+ } else {
|
|
112
|
+ String classname = getCellValue(cell);
|
|
113
|
+ if(!classNames.contains(classname)){
|
|
114
|
+ throw new Exception("第" + (x + 1) + "行班级不存在");
|
|
115
|
+ }else{
|
|
116
|
+ stu.setClassname(classname);
|
|
117
|
+ }
|
|
118
|
+ }
|
|
119
|
+ } else if (y == 3) {//准考证号
|
|
120
|
+ if (isEmptyCell(cell)) {
|
|
121
|
+ throw new Exception("第" + (x + 1) + "行准考证号不能为空");
|
|
122
|
+ } else {
|
|
123
|
+ String examno = getCellValue(cell);
|
|
124
|
+ if(!stuMap.containsKey(examno)) {
|
|
125
|
+ throw new Exception("第" + (x + 1) + "行准考证号不存在");
|
|
126
|
+ }else{
|
|
127
|
+ stu.setExamno(examno);
|
|
128
|
+ }
|
|
129
|
+
|
|
130
|
+ }
|
|
131
|
+ } else if (y == 4) {//补录成绩
|
|
132
|
+ String fillScore = getCellValue(cell);
|
|
133
|
+ if(N_Utils.isEmpty(fillScore)){
|
|
134
|
+ stu.setStuscore(null);
|
|
135
|
+ }else{
|
|
136
|
+ stu.setStuscore(Double.parseDouble(fillScore));
|
|
137
|
+ }
|
|
138
|
+ }
|
|
139
|
+ }
|
|
140
|
+ //判断学生信息对应用户是否存在
|
|
141
|
+ PaperStuScoreVo oldStu = stuMap.get(stu.getExamno());
|
|
142
|
+ if(stu.getStuscore() !=null && oldStu.getStuscoretype() != 1){
|
|
143
|
+ //说明设置了分数,则进行用户其他信息教研
|
|
144
|
+ if(!stu.getStudentname().equals(oldStu.getStudentname()) || !stu.getClassname().equals(oldStu.getClassname())){
|
|
145
|
+ throw new Exception("第" + (x + 1) + "行学生不存在或没有参加本次考试");
|
|
146
|
+ }else{
|
|
147
|
+ MsPaperStudentScore score = setPaperStudentScore(oldStu,subject.getSubjectid(),stu.getStuscore(),createid,timestamp);
|
|
148
|
+ rtnList.add(score);
|
|
149
|
+ }
|
|
150
|
+ }
|
|
151
|
+ }
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+ } catch (Exception e) {
|
|
155
|
+ log.error("读取Excel文件失败", e.getMessage());
|
|
156
|
+ throw new Exception("读取Excel文件失败");
|
|
157
|
+ }
|
|
158
|
+ return rtnList;
|
|
159
|
+ }
|
|
160
|
+
|
|
161
|
+ private static MsPaperStudentScore setPaperStudentScore(PaperStuScoreVo oldStu, String subjectid, double stuscore, Integer createid, Integer timestamp) {
|
|
162
|
+ MsPaperStudentScore stuScore = new MsPaperStudentScore();
|
|
163
|
+ stuScore.setExamid(oldStu.getExamid());
|
|
164
|
+ stuScore.setMpid(oldStu.getMpid());
|
|
165
|
+ stuScore.setSubjectid(subjectid);
|
|
166
|
+ stuScore.setStudentid(oldStu.getStudentid());
|
|
167
|
+ stuScore.setFillpre(oldStu.getStuscore());
|
|
168
|
+ stuScore.setFillscore(stuscore);
|
|
169
|
+ stuScore.setCreateid(createid);
|
|
170
|
+ stuScore.setCreatetime(timestamp);
|
|
171
|
+ return stuScore;
|
|
172
|
+ }
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+ /**
|
|
176
|
+ * 根据文件后缀,自适应上传文件的版本
|
|
177
|
+ *
|
|
178
|
+ * @return org.apache.poi.ss.usermodel.Workbook
|
|
179
|
+ * @Param [inStr, fileName]
|
|
180
|
+ * @Author ywx
|
|
181
|
+ * @Date 2020/6/3 16:42
|
|
182
|
+ **/
|
|
183
|
+ public static Workbook getWorkbook(InputStream inStr, String fileName) throws Exception {
|
|
184
|
+ Workbook wb;
|
|
185
|
+ String fileType = fileName.substring(fileName.lastIndexOf("."));
|
|
186
|
+ if (excel2003L.equals(fileType)) {
|
|
187
|
+ wb = new HSSFWorkbook(inStr); //2003-
|
|
188
|
+ } else if (excel2007U.equals(fileType)) {
|
|
189
|
+ wb = new XSSFWorkbook(inStr); //2007+
|
|
190
|
+ } else {
|
|
191
|
+ throw new Exception("Excel格式解析有误!");
|
|
192
|
+ }
|
|
193
|
+ return wb;
|
|
194
|
+ }
|
|
195
|
+
|
|
196
|
+ /**
|
|
197
|
+ * 判断单元格是否是空值
|
|
198
|
+ *
|
|
199
|
+ * @return java.lang.Boolean
|
|
200
|
+ * @Param [cell]
|
|
201
|
+ * @Author ywx
|
|
202
|
+ * @Date 2020/6/3 16:37
|
|
203
|
+ **/
|
|
204
|
+ public static Boolean isEmptyCell(Cell cell) {
|
|
205
|
+ if (cell != null) {
|
|
206
|
+ Boolean rtn = true;
|
|
207
|
+ if (N_Utils.isNotEmpty(getCellValue(cell))) {
|
|
208
|
+ rtn = false;
|
|
209
|
+ }
|
|
210
|
+ return rtn;
|
|
211
|
+ } else {
|
|
212
|
+ return true;
|
|
213
|
+ }
|
|
214
|
+ }
|
|
215
|
+
|
|
216
|
+ /**
|
|
217
|
+ * 对表格中数值进行格式化
|
|
218
|
+ *
|
|
219
|
+ * @return java.lang.Object
|
|
220
|
+ * @Param [cell]
|
|
221
|
+ * @Author ywx
|
|
222
|
+ * @Date 2020/6/3 16:41
|
|
223
|
+ **/
|
|
224
|
+ public static String getCellValue(Cell cell) {
|
|
225
|
+ String value = null;
|
|
226
|
+ if (cell == null) return value;
|
|
227
|
+ DecimalFormat df = new DecimalFormat("0"); //格式化number String字符
|
|
228
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //日期格式化
|
|
229
|
+ DecimalFormat df2 = new DecimalFormat("0"); //格式化数字
|
|
230
|
+ switch (cell.getCellTypeEnum()) {
|
|
231
|
+ case STRING:
|
|
232
|
+ value = cell.getRichStringCellValue().getString();
|
|
233
|
+ break;
|
|
234
|
+ case NUMERIC:
|
|
235
|
+ String cellStyle = cell.getCellStyle().getDataFormatString();
|
|
236
|
+ if ("General".equals(cellStyle)) {
|
|
237
|
+ Double d = cell.getNumericCellValue();
|
|
238
|
+ if (d.toString().contains("E")) {
|
|
239
|
+ cell.setCellType(CellType.STRING);
|
|
240
|
+ value = cell.getStringCellValue();
|
|
241
|
+ } else {
|
|
242
|
+ value = df.format(d);
|
|
243
|
+ }
|
|
244
|
+ } else if ("m/d/yy".equals(cellStyle)) {
|
|
245
|
+ value = sdf.format(cell.getDateCellValue());
|
|
246
|
+ } else {
|
|
247
|
+ // 处理数字单元格,可能是整数或双精度浮点数
|
|
248
|
+ if (cell.getCellStyle().getDataFormat() == 0) {
|
|
249
|
+ // 假定没有特殊的数字格式,则处理为双精度浮点数
|
|
250
|
+ value = cell.getNumericCellValue()+"";
|
|
251
|
+ } else {
|
|
252
|
+ value = df2.format(cell.getNumericCellValue());
|
|
253
|
+ }
|
|
254
|
+ }
|
|
255
|
+ break;
|
|
256
|
+ case BOOLEAN:
|
|
257
|
+ boolean rtn = cell.getBooleanCellValue();
|
|
258
|
+ if (rtn) {
|
|
259
|
+ value = "1";//真
|
|
260
|
+ } else {
|
|
261
|
+ value = "0";//假
|
|
262
|
+ }
|
|
263
|
+ break;
|
|
264
|
+ case BLANK:
|
|
265
|
+ value = "";
|
|
266
|
+ break;
|
|
267
|
+ case FORMULA://读取excel公式的值
|
|
268
|
+ try {
|
|
269
|
+ value = String.valueOf(cell.getNumericCellValue());
|
|
270
|
+ } catch (IllegalStateException e) {
|
|
271
|
+ value = String.valueOf(cell.getRichStringCellValue());
|
|
272
|
+ }
|
|
273
|
+ break;
|
|
274
|
+ default:
|
|
275
|
+ break;
|
|
276
|
+ }
|
|
277
|
+ return N_Utils.strTrim(value);
|
|
278
|
+ }
|
|
279
|
+
|
|
280
|
+ /**
|
|
281
|
+ * 检验Excel内容是否为空
|
|
282
|
+ *
|
|
283
|
+ * @return void
|
|
284
|
+ * @Param [file]
|
|
285
|
+ * @Author ywx
|
|
286
|
+ * @Date 2020/6/3 16:36
|
|
287
|
+ **/
|
|
288
|
+ public static void isExcelEmpty(MultipartFile file) throws Exception {
|
|
289
|
+ if (null == file || file.isEmpty()) {
|
|
290
|
+ throw new Exception("文件不存在!");
|
|
291
|
+ }
|
|
292
|
+ InputStream in = file.getInputStream();
|
|
293
|
+ String fileName = file.getOriginalFilename();
|
|
294
|
+ Workbook work = getWorkbook(in, fileName);
|
|
295
|
+ if (null == work) {
|
|
296
|
+ throw new Exception("Excel内容为空!");
|
|
297
|
+ }
|
|
298
|
+ Integer sheet_size = work.getNumberOfSheets();
|
|
299
|
+ if (sheet_size == 0) {
|
|
300
|
+ throw new Exception("Excel内容为空!");
|
|
301
|
+ }
|
|
302
|
+ }
|
|
303
|
+}
|