|
|
@ -3,15 +3,14 @@ package com.example.attendance.service.impl;
|
|
|
|
import com.example.attendance.entity.Student;
|
|
|
|
import com.example.attendance.entity.Student;
|
|
|
|
import com.example.attendance.mapper.StudentMapper;
|
|
|
|
import com.example.attendance.mapper.StudentMapper;
|
|
|
|
import com.example.attendance.service.StudentService;
|
|
|
|
import com.example.attendance.service.StudentService;
|
|
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
@ -33,6 +32,9 @@ public class StudentServiceImpl implements StudentService {
|
|
|
|
// 读取第一个工作表
|
|
|
|
// 读取第一个工作表
|
|
|
|
Sheet sheet = workbook.getSheetAt(0);
|
|
|
|
Sheet sheet = workbook.getSheetAt(0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查文件中是否包含积分列
|
|
|
|
|
|
|
|
boolean hasPointsColumn = sheet.getRow(0).getLastCellNum() > 2;
|
|
|
|
|
|
|
|
|
|
|
|
// 遍历每一行,从第二行开始(假设第一行是标题)
|
|
|
|
// 遍历每一行,从第二行开始(假设第一行是标题)
|
|
|
|
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
|
|
|
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
|
|
|
Row row = sheet.getRow(i);
|
|
|
|
Row row = sheet.getRow(i);
|
|
|
@ -41,7 +43,15 @@ public class StudentServiceImpl implements StudentService {
|
|
|
|
// 读取学生数据
|
|
|
|
// 读取学生数据
|
|
|
|
String studentNumber = row.getCell(0).getStringCellValue();
|
|
|
|
String studentNumber = row.getCell(0).getStringCellValue();
|
|
|
|
String name = row.getCell(1).getStringCellValue();
|
|
|
|
String name = row.getCell(1).getStringCellValue();
|
|
|
|
BigDecimal points = new BigDecimal(row.getCell(2).getNumericCellValue());
|
|
|
|
|
|
|
|
|
|
|
|
// 如果有积分列,读取积分,如果没有积分列或者积分为空,则默认积分为 0
|
|
|
|
|
|
|
|
BigDecimal points = BigDecimal.ZERO;
|
|
|
|
|
|
|
|
if (hasPointsColumn && row.getCell(2) != null) {
|
|
|
|
|
|
|
|
Cell pointsCell = row.getCell(2);
|
|
|
|
|
|
|
|
if (pointsCell.getCellType() == CellType.NUMERIC) {
|
|
|
|
|
|
|
|
points = new BigDecimal(pointsCell.getNumericCellValue());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Student student = new Student();
|
|
|
|
Student student = new Student();
|
|
|
|
student.setStudentNumber(studentNumber);
|
|
|
|
student.setStudentNumber(studentNumber);
|
|
|
@ -56,6 +66,33 @@ public class StudentServiceImpl implements StudentService {
|
|
|
|
studentMapper.saveStudents(students);
|
|
|
|
studentMapper.saveStudents(students);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void exportStudents(OutputStream outputStream) throws Exception {
|
|
|
|
|
|
|
|
List<Student> students = studentMapper.findAll();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try (Workbook workbook = new XSSFWorkbook()) {
|
|
|
|
|
|
|
|
Sheet sheet = workbook.createSheet("Students");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建标题行
|
|
|
|
|
|
|
|
Row headerRow = sheet.createRow(0);
|
|
|
|
|
|
|
|
headerRow.createCell(0).setCellValue("学号");
|
|
|
|
|
|
|
|
headerRow.createCell(1).setCellValue("姓名");
|
|
|
|
|
|
|
|
headerRow.createCell(2).setCellValue("积分");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 填充数据
|
|
|
|
|
|
|
|
int rowNum = 1;
|
|
|
|
|
|
|
|
for (Student student : students) {
|
|
|
|
|
|
|
|
Row row = sheet.createRow(rowNum++);
|
|
|
|
|
|
|
|
row.createCell(0).setCellValue(student.getStudentNumber());
|
|
|
|
|
|
|
|
row.createCell(1).setCellValue(student.getName());
|
|
|
|
|
|
|
|
row.createCell(2).setCellValue(student.getPoints().doubleValue());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 将数据写入输出流
|
|
|
|
|
|
|
|
workbook.write(outputStream);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 调整指定学生的积分
|
|
|
|
* 调整指定学生的积分
|
|
|
|
* @param studentNumber 学生编号
|
|
|
|
* @param studentNumber 学生编号
|
|
|
|