From 680ca6808f48d11452ab577de1f376c3bfdf3383 Mon Sep 17 00:00:00 2001 From: Spark <2666652@gmail.com> Date: Sat, 12 Oct 2024 00:49:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=AD=A6=E7=94=9F?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E8=A1=A8=E7=9A=84=E5=AF=BC=E5=85=A5=E5=92=8C?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aspark/controller/StudentController.java | 13 ++- .../cc/aspark/service/StudentService.java | 4 + .../service/impl/StudentServiceImpl.java | 104 ++++++++++++++++++ 3 files changed, 120 insertions(+), 1 deletion(-) diff --git a/RollCallServer/src/main/java/cc/aspark/controller/StudentController.java b/RollCallServer/src/main/java/cc/aspark/controller/StudentController.java index 5154c05..dc5d436 100644 --- a/RollCallServer/src/main/java/cc/aspark/controller/StudentController.java +++ b/RollCallServer/src/main/java/cc/aspark/controller/StudentController.java @@ -57,6 +57,18 @@ public class StudentController { return Result.success(); } + @Operation(summary = "通过文件导入学生信息") + @PostMapping("/import") + public Result saveBatch(MultipartFile file) { + studentService.importStudentsByExcel(file); + return Result.success(); + } + + @Operation(summary = "导出学生信息") + @GetMapping("/export") + public void exportStudents(HttpServletResponse response) { + studentService.exportStudentsToExcel(response); + } @Operation(summary = "更新学生信息") @PutMapping @@ -73,5 +85,4 @@ public class StudentController { } - } diff --git a/RollCallServer/src/main/java/cc/aspark/service/StudentService.java b/RollCallServer/src/main/java/cc/aspark/service/StudentService.java index 6b5cd28..81e3a40 100644 --- a/RollCallServer/src/main/java/cc/aspark/service/StudentService.java +++ b/RollCallServer/src/main/java/cc/aspark/service/StudentService.java @@ -21,4 +21,8 @@ public interface StudentService extends IService { void save(StudentDTO studentDTO); + void importStudentsByExcel(MultipartFile file); + + void exportStudentsToExcel(HttpServletResponse response); + } diff --git a/RollCallServer/src/main/java/cc/aspark/service/impl/StudentServiceImpl.java b/RollCallServer/src/main/java/cc/aspark/service/impl/StudentServiceImpl.java index f464e3c..5cf98c4 100644 --- a/RollCallServer/src/main/java/cc/aspark/service/impl/StudentServiceImpl.java +++ b/RollCallServer/src/main/java/cc/aspark/service/impl/StudentServiceImpl.java @@ -128,4 +128,108 @@ public class StudentServiceImpl extends ServiceImpl } + @Override + public void importStudentsByExcel(MultipartFile file) { + try { + ExcelReader reader = ExcelUtil.getReader(file.getInputStream()); + reader .addHeaderAlias("学号", "no") + .addHeaderAlias("姓名", "name") + .addHeaderAlias("性别", "gender") + .addHeaderAlias("班级", "className") + .addHeaderAlias("积分", "credits") + .addHeaderAlias("创建时间", "createTime") + .addHeaderAlias("最后操作时间", "updateTime"); + List> rows = reader.readAll(); + LocalDateTime now = LocalDateTime.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + List studentList = rows.stream().map(row -> { + Student student = new Student(); + student.setNo(row.get("no").toString()); + student.setName(row.get("name").toString()); + student.setGender("男".equals(row.get("gender")) ? (short) 0 : (short) 1); + student.setClassName(row.get("className").toString()); + student.setCreator(Integer.valueOf(BaseContext.getCurrentId().toString())); + if (row.get("credits") == null) { + student.setPoints(0.00); + } else { + student.setPoints(Double.valueOf(row.get("credits").toString())); + } + + if (row.get("createTime") == null) { + student.setCreateTime(now); + } else { + String createTimeStr = row.get("createTime").toString(); + student.setCreateTime(LocalDateTime.parse(createTimeStr, formatter)); + } + + if (row.get("updateTime") == null) { + student.setUpdateTime(now); + } else { + String updateTimeStr = row.get("updateTime").toString(); + student.setUpdateTime(LocalDateTime.parse(updateTimeStr, formatter)); + } + return student; + }).toList(); + try { + baseMapper.insertOrUpdate(studentList); + } catch (Exception e) { + log.info(e.getMessage()); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void exportStudentsToExcel(HttpServletResponse response) { + try { + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + wrapper.eq(Student::getCreator, BaseContext.getCurrentId()); + List studentList = baseMapper.selectList(wrapper); + + // 创建一个Excel写入器 + ExcelWriter writer = ExcelUtil.getWriter(true); + + // 设置表头别名 + writer.addHeaderAlias("no", "学号"); + writer.addHeaderAlias("name", "姓名"); + writer.addHeaderAlias("gender", "性别"); + writer.addHeaderAlias("className", "班级"); + writer.addHeaderAlias("points", "积分"); + writer.addHeaderAlias("createTime", "创建时间"); + writer.addHeaderAlias("updateTime", "最后操作时间"); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + // 将性别的数字转换为文字 + List> rows = studentList.stream().map(student -> { + Map row = new HashMap<>(); + row.put("no", student.getNo()); + row.put("name", student.getName()); + row.put("gender", student.getGender() == 0 ? "男" : "女"); + row.put("className", student.getClassName()); + row.put("points", student.getPoints()); + row.put("createTime", student.getCreateTime().format(formatter)); + row.put("updateTime", student.getUpdateTime().format(formatter)); + return row; + }).collect(Collectors.toList()); + + // 写入数据 + writer.write(rows, true); + + // 设置响应头 + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"); + response.setHeader("Content-Disposition", "attachment;filename=students.xlsx"); + + // 输出Excel文件 + ServletOutputStream out = response.getOutputStream(); + writer.flush(out, true); + writer.close(); + IoUtil.close(out); + + } catch (Exception e) { + log.error("导出Excel失败", e); + throw new BaseException(e.getMessage()); + } + } + } \ No newline at end of file