diff --git a/src/main/java/com/example/attendance/controller/StudentController.java b/src/main/java/com/example/attendance/controller/StudentController.java index 4d3df24..0cc0444 100644 --- a/src/main/java/com/example/attendance/controller/StudentController.java +++ b/src/main/java/com/example/attendance/controller/StudentController.java @@ -3,11 +3,20 @@ package com.example.attendance.controller; import com.example.attendance.entity.PointsRequest; import com.example.attendance.entity.Student; import com.example.attendance.service.StudentService; +import jakarta.servlet.http.HttpServletResponse; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.util.List; @RestController @@ -105,4 +114,55 @@ public class StudentController { return "学生数据导入失败:" + e.getMessage(); } } + /*导入学生数据 + */ + @GetMapping("/export-students") + public ResponseEntity exportStudents() throws IOException { + List students = studentService.findAll(); // 获取所有学生数据 + + // 创建 Excel 工作簿 + Workbook workbook = new XSSFWorkbook(); + Sheet sheet = workbook.createSheet("Students"); + + // 创建表头 + Row header = sheet.createRow(0); + header.createCell(0).setCellValue("学号"); + header.createCell(1).setCellValue("姓名"); + header.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()); + } + + // 将 Excel 文件写入 ByteArrayOutputStream + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + workbook.write(outputStream); + workbook.close(); + + // 设置 HTTP 头信息 + HttpHeaders headers = new HttpHeaders(); + headers.setContentDispositionFormData("attachment", "students.xlsx"); + headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); + + // 返回文件内容作为响应 + return ResponseEntity + .ok() + .headers(headers) + .body(outputStream.toByteArray()); + } + /** + * 获取Excel模板下载链接 + * @return 包含下载链接的响应 + */ + @GetMapping("/download-template") + public ResponseEntity getTemplateDownloadLink() { + // 返回百度网盘的模板下载链接 + String downloadLink = "https://pan.baidu.com/s/1NUukdPo4qUVbM4V9MWTx2g?pwd=1234"; + return ResponseEntity.ok(downloadLink); + } } diff --git a/src/main/java/com/example/attendance/service/StudentService.java b/src/main/java/com/example/attendance/service/StudentService.java index 35eefb0..6d5416b 100644 --- a/src/main/java/com/example/attendance/service/StudentService.java +++ b/src/main/java/com/example/attendance/service/StudentService.java @@ -3,11 +3,13 @@ package com.example.attendance.service; import com.example.attendance.entity.Student; import org.springframework.web.multipart.MultipartFile; +import java.io.OutputStream; import java.math.BigDecimal; import java.util.List; public interface StudentService { void importStudents(MultipartFile file) throws Exception ; + void exportStudents(OutputStream outputStream) throws Exception; void adjustPoints(String studentNumber, BigDecimal pointsDelta); List getStudentRanking(int page, int size); Student findById(Long id); diff --git a/src/main/java/com/example/attendance/service/impl/StudentServiceImpl.java b/src/main/java/com/example/attendance/service/impl/StudentServiceImpl.java index af44988..b46f396 100644 --- a/src/main/java/com/example/attendance/service/impl/StudentServiceImpl.java +++ b/src/main/java/com/example/attendance/service/impl/StudentServiceImpl.java @@ -3,15 +3,14 @@ package com.example.attendance.service.impl; import com.example.attendance.entity.Student; import com.example.attendance.mapper.StudentMapper; import com.example.attendance.service.StudentService; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.InputStream; +import java.io.OutputStream; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @@ -33,6 +32,9 @@ public class StudentServiceImpl implements StudentService { // 读取第一个工作表 Sheet sheet = workbook.getSheetAt(0); + // 检查文件中是否包含积分列 + boolean hasPointsColumn = sheet.getRow(0).getLastCellNum() > 2; + // 遍历每一行,从第二行开始(假设第一行是标题) for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); @@ -41,7 +43,15 @@ public class StudentServiceImpl implements StudentService { // 读取学生数据 String studentNumber = row.getCell(0).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.setStudentNumber(studentNumber); @@ -56,6 +66,33 @@ public class StudentServiceImpl implements StudentService { studentMapper.saveStudents(students); } + @Override + public void exportStudents(OutputStream outputStream) throws Exception { + List 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 学生编号