Browse Source

学生、教师导入添加标题头校验、读取科学计数法数字

tags/正式版本
雍文秀 2 years ago
parent
commit
a17f64c829
1 changed files with 32 additions and 5 deletions
  1. 32
    5
      suser/src/main/java/com/xhkjedu/suser/utils/PoiUtils.java

+ 32
- 5
suser/src/main/java/com/xhkjedu/suser/utils/PoiUtils.java View File

@@ -5,10 +5,7 @@ import com.xhkjedu.utils.MD5;
5 5
 import com.xhkjedu.utils.N_Utils;
6 6
 import lombok.extern.slf4j.Slf4j;
7 7
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
8
-import org.apache.poi.ss.usermodel.Cell;
9
-import org.apache.poi.ss.usermodel.Row;
10
-import org.apache.poi.ss.usermodel.Sheet;
11
-import org.apache.poi.ss.usermodel.Workbook;
8
+import org.apache.poi.ss.usermodel.*;
12 9
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
13 10
 import org.springframework.stereotype.Component;
14 11
 import org.springframework.web.multipart.MultipartFile;
@@ -60,6 +57,17 @@ public class PoiUtils {
60 57
 
61 58
             int rows = sheet.getLastRowNum(); // 获取最后一个实际行的下标,比行数小1
62 59
             int firstRowNum = sheet.getFirstRowNum(); // 获取第一个实际行的下标,(firstRowNum=0)
60
+            row = sheet.getRow(firstRowNum);//标题行
61
+            String titleName = "姓名登录学号性别年级班级准考证号";//标题格式
62
+            StringBuilder sb = new StringBuilder();
63
+            sb.append(row.getCell(0));//姓名
64
+            sb.append(row.getCell(1));//登录学号
65
+            sb.append(row.getCell(2));//性别
66
+            sb.append(row.getCell(3));//年级
67
+            sb.append(row.getCell(4));//班级
68
+            sb.append(row.getCell(5));//准考证号
69
+            String importTitleName = sb.toString().replaceAll(" |(必填)", "");
70
+            if(!titleName.equals(importTitleName)) throw new Exception("标题格式不正确");
63 71
 
64 72
             // 导入用户信息
65 73
             List<TUser> users = new ArrayList<>();
@@ -203,6 +211,19 @@ public class PoiUtils {
203 211
 
204 212
             int rows = sheet.getLastRowNum(); // 获取最后一个实际行的下标,比行数小1
205 213
             int firstRowNum = sheet.getFirstRowNum(); // 获取第一个实际行的下标,(firstRowNum=0)
214
+            row = sheet.getRow(firstRowNum);//标题行
215
+            String titleName = "姓名登录名性别手机号出生日期身份证号年级班级";//标题格式
216
+            StringBuilder sb = new StringBuilder();
217
+            sb.append(row.getCell(0));//姓名
218
+            sb.append(row.getCell(1));//登录名
219
+            sb.append(row.getCell(2));//性别
220
+            sb.append(row.getCell(3));//手机号
221
+            sb.append(row.getCell(4));//出生日期
222
+            sb.append(row.getCell(5));//身份证号
223
+            sb.append(row.getCell(6));//年级
224
+            sb.append(row.getCell(7));//班级
225
+            String importTitleName = sb.toString().replaceAll(" |(必填)", "");
226
+            if(!titleName.equals(importTitleName)) throw new Exception("标题格式不正确");
206 227
 
207 228
             // 导入用户信息
208 229
             List<TUser> users = new ArrayList<>();
@@ -402,7 +423,13 @@ public class PoiUtils {
402 423
             case NUMERIC:
403 424
                 String cellStyle = cell.getCellStyle().getDataFormatString();
404 425
                 if ("General".equals(cellStyle)) {
405
-                    value = df.format(cell.getNumericCellValue());
426
+                    Double d = cell.getNumericCellValue();
427
+                    if (d.toString().contains("E")) {
428
+                        cell.setCellType(CellType.STRING);
429
+                        value = cell.getStringCellValue();
430
+                    } else {
431
+                        value = df.format(d);
432
+                    }
406 433
                 } else if ("m/d/yy".equals(cellStyle)) {
407 434
                     value = sdf.format(cell.getDateCellValue());
408 435
                 } else {

Loading…
Cancel
Save