Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0682a43

Browse files
committed
代码提交,批量导入Excel
1 parent 8575d59 commit 0682a43

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

spring-poi-demo/pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
<dependency>
5353
<groupId>cn.afterturn</groupId>
5454
<artifactId>easypoi-spring-boot-starter</artifactId>
55-
<version>3.3.0</version>
55+
<!--<version>3.3.0</version>-->
56+
<version>4.0.0</version>
57+
<!--<version>4.1.0</version>-->
5658
</dependency>
5759
<!--easypoi-->
5860
<!--参数校验类-->

spring-poi-demo/src/main/java/com/jay/model/Student.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jay.model;
22

3+
import cn.afterturn.easypoi.excel.annotation.Excel;
34
import lombok.Data;
45

56
/**
@@ -12,22 +13,27 @@ public class Student {
1213
/**
1314
* 班级名称
1415
*/
16+
@Excel(name="班级名称")
1517
private String className;
1618

1719
/**
1820
* 学生姓名
1921
*/
22+
@Excel(name = "学生姓名",orderNum = "1")
2023
private String studentName;
2124
/**
2225
* 学生手机号
2326
*/
27+
@Excel(name = "学生手机号",orderNum = "2")
2428
private String studentMobile;
2529
/**
2630
* 学生身份证
2731
*/
32+
@Excel(name = "学生身份证号",orderNum = "3")
2833
private String idCard;
2934
/**
3035
* 学号
3136
*/
37+
@Excel(name = "学号",orderNum = "4")
3238
private String studentNo;
3339
}

spring-poi-demo/src/main/java/com/jay/util/ExcelUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.apache.commons.lang3.StringUtils;
99
import org.apache.poi.ss.formula.functions.T;
1010
import org.apache.poi.ss.usermodel.Cell;
11+
import org.apache.poi.ss.usermodel.CellType;
1112
import org.apache.poi.ss.usermodel.Row;
1213
import org.apache.poi.ss.usermodel.Sheet;
1314
import org.apache.poi.ss.usermodel.Workbook;
@@ -129,7 +130,7 @@ public Workbook getWorkbook(Class<T> clzss, List<T> list, String sheetName) {
129130
public static String getKeyValue(Cell cell) {
130131
try {
131132
String value = PoiCellUtil.getCellValue(cell);
132-
if (cell != null && cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
133+
if (cell != null && cell.getCellType() == CellType.NUMERIC) {
133134
return ExcelUtil.getDecimalFormat().format(cell.getNumericCellValue());
134135
}
135136
return StringUtils.isBlank(value) ? null : value.trim();
@@ -146,7 +147,7 @@ public static String getKeyValue(Cell cell) {
146147
*/
147148
public static String getMoney(Cell cell) {
148149
String value = PoiCellUtil.getCellValue(cell);
149-
if (cell != null && cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
150+
if (cell != null && cell.getCellType() == CellType.NUMERIC) {
150151
return String.valueOf(cell.getNumericCellValue());
151152
}
152153
return StringUtils.isBlank(value) ? null : value.trim();

spring-poi-demo/src/main/java/com/jay/util/StudentExcelImportWrapper.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
public class StudentExcelImportWrapper {
2121

2222

23-
private static final ForkJoinPool forkJoinPool = new ForkJoinPool(4);
24-
2523

2624
public List<Student> importExcel(Workbook workbook) {
27-
ForkJoinPool forkJoinPool = StudentExcelImportWrapper.forkJoinPool;
25+
ForkJoinPool forkJoinPool = new ForkJoinPool(4);;
2826
Sheet sheet = workbook.getSheetAt(0);
2927
JoinTask joinTask = new JoinTask(1, sheet.getLastRowNum(), sheet);
3028
List<Student> importVOList = forkJoinPool.invoke(joinTask);
@@ -84,23 +82,22 @@ protected List<Student> compute() {
8482
private List<Student> getData(Sheet sheet, int start, int end) {
8583
List<Student> mapList = new ArrayList<>();
8684
for (int i = start; i <= end; i++) {
87-
Student Student = null;
85+
Student student = null;
8886
try {
8987
Row row = sheet.getRow(i);
90-
Student = new Student();
91-
Student.setClassName(ExcelUtil.getKeyValue(row.getCell(0)));
92-
Student.setStudentName(ExcelUtil.getKeyValue(row.getCell(1)));
93-
Student.setStudentMobile(ExcelUtil.getKeyValue(row.getCell(2)));
94-
Student.setIdCard(ExcelUtil.getKeyValue(row.getCell(3)));
95-
Student.setStudentNo(ExcelUtil.getKeyValue(row.getCell(4)));
96-
Student.setIdCard(ExcelUtil.getKeyValue(row.getCell(5)));
88+
student = new Student();
89+
student.setClassName(ExcelUtil.getKeyValue(row.getCell(0)));
90+
student.setStudentName(ExcelUtil.getKeyValue(row.getCell(1)));
91+
student.setStudentMobile(ExcelUtil.getKeyValue(row.getCell(2)));
92+
student.setIdCard(ExcelUtil.getKeyValue(row.getCell(3)));
93+
student.setStudentNo(ExcelUtil.getKeyValue(row.getCell(4)));
94+
student.setIdCard(ExcelUtil.getKeyValue(row.getCell(5)));
9795
} catch (Exception e) {
9896
log.info("***************税号={},文件名={},数据解析出现异常={}", e);
9997
continue;
10098
}
101-
mapList.add(Student);
99+
mapList.add(student);
102100
}
103101
return mapList;
104102
}
105-
106103
}

0 commit comments

Comments
 (0)