diff --git a/project2/.idea/artifacts/project2_war_exploded.xml b/project2/.idea/artifacts/project2_war_exploded.xml index 0cf82b9..7143a7d 100644 --- a/project2/.idea/artifacts/project2_war_exploded.xml +++ b/project2/.idea/artifacts/project2_war_exploded.xml @@ -46,6 +46,8 @@ + + diff --git a/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/controller/OutputController.class b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/controller/OutputController.class new file mode 100644 index 0000000..92aa92d Binary files /dev/null and b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/controller/OutputController.class differ diff --git a/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/spring-security.xml b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/spring-security.xml index 412183f..55b44eb 100644 --- a/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/spring-security.xml +++ b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/spring-security.xml @@ -23,6 +23,8 @@ + +
- +
+
+
+ +
+
+
+
+ +
+
+
@@ -166,7 +178,18 @@ - + diff --git a/project2/out/artifacts/project2_war_exploded/WEB-INF/views/scoreList.jsp b/project2/out/artifacts/project2_war_exploded/WEB-INF/views/scoreList.jsp index 7987f95..2687973 100644 --- a/project2/out/artifacts/project2_war_exploded/WEB-INF/views/scoreList.jsp +++ b/project2/out/artifacts/project2_war_exploded/WEB-INF/views/scoreList.jsp @@ -130,7 +130,20 @@ style="background-image: url('${pageContext.request.contextPath}/resources/img/jason-blackeye-nyL-rzwP-Mk-unsplash.jpg'); margin-top: -58.59px;">
- +
+
+
+ +
+
+
+
+ +
+
+
+
@@ -138,6 +151,8 @@ + + @@ -147,6 +162,8 @@ + + @@ -168,7 +185,18 @@ - + diff --git a/project2/out/artifacts/project2_war_exploded/WEB-INF/views/studentList.jsp b/project2/out/artifacts/project2_war_exploded/WEB-INF/views/studentList.jsp index 78401b3..fa83aec 100644 --- a/project2/out/artifacts/project2_war_exploded/WEB-INF/views/studentList.jsp +++ b/project2/out/artifacts/project2_war_exploded/WEB-INF/views/studentList.jsp @@ -130,7 +130,19 @@ style="background-image: url('${pageContext.request.contextPath}/resources/img/jason-blackeye-nyL-rzwP-Mk-unsplash.jpg'); margin-top: -58.59px;">
- +
+
+
+ +
+
+
+
+ +
+
+
成绩ID 学号学生姓名课程ID 课程名 成绩
${score.scoreID} ${score.student.id}${score.student.name}${score.course.courseID} ${score.course.courseName} ${score.score}
@@ -172,7 +184,18 @@ - + diff --git a/project2/pom.xml b/project2/pom.xml index fc9ef1e..2d7a384 100644 --- a/project2/pom.xml +++ b/project2/pom.xml @@ -156,5 +156,15 @@ log4j-core2.12.4 + + org.apache.commons + commons-csv + 1.10.0 + + + commons-io + commons-io + 2.12.0 + \ No newline at end of file diff --git a/project2/src/main/java/cyou/chenx221/controller/OutputController.java b/project2/src/main/java/cyou/chenx221/controller/OutputController.java new file mode 100644 index 0000000..27260c4 --- /dev/null +++ b/project2/src/main/java/cyou/chenx221/controller/OutputController.java @@ -0,0 +1,127 @@ +package cyou.chenx221.controller; + +import cyou.chenx221.pojo.Course; +import cyou.chenx221.pojo.Score; +import cyou.chenx221.pojo.Student; +import cyou.chenx221.service.CourseService; +import cyou.chenx221.service.ScoreService; +import cyou.chenx221.service.StudentService; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +@RestController +@RequestMapping("/output") +public class OutputController { + + @Autowired + private StudentService studentService; + + @Autowired + private CourseService courseService; + + @Autowired + private ScoreService scoreService; + + @GetMapping("/export-csv") + public void exportCSV(HttpServletResponse response, @RequestParam("type") String type) throws IOException { + response.setHeader("Content-Disposition", "attachment; filename=export.csv"); + response.setContentType("text/csv; charset=UTF-8"); // 设置字符编码为UTF-8 + response.setCharacterEncoding("UTF-8"); // 设置响应字符编码为UTF-8 + + List headers; + List> data; + + switch (type) { + case "student" -> { + headers = Arrays.asList("学生ID", "学生姓名", "性别", "出生日期", "联系方式", "班级"); + List students = studentService.getAllStudents(); // 根据实际情况获取学生数据 + data = transformStudentsToData(students); + } + case "course" -> { + headers = Arrays.asList("课程ID", "课程名", "课程描述"); + List courses = courseService.getAllCourses(); // 根据实际情况获取课程数据 + data = transformCoursesToData(courses); + } + case "score" -> { + headers = Arrays.asList("成绩ID", "学生ID", "学生姓名", "课程ID", "课程名称", "成绩"); + List scores = scoreService.getAllScores(); // 根据实际情况获取成绩数据 + data = transformScoresToData(scores); + } + default -> throw new IllegalArgumentException("Invalid export type: " + type); + } + + // 使用 Apache Commons CSV 构建 CSV 输出 + try (OutputStream outputStream = response.getOutputStream(); + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); + CSVPrinter csvPrinter = new CSVPrinter(outputStreamWriter, CSVFormat.DEFAULT)) { + csvPrinter.printRecord(headers); + for (List row : data) { + csvPrinter.printRecord(row); + } + csvPrinter.flush(); + } + } + + // 辅助方法:将学生对象列表转换为数据列表 + private List> transformStudentsToData(List students) { + List> data = new ArrayList<>(); + for (Student student : students) { + List rowData = Arrays.asList( + String.valueOf(student.getId()), + student.getName(), + student.getSex(), + student.getBirthday().toString(), + String.valueOf(student.getPhone()), + String.valueOf(student.getClasses()) + ); + data.add(rowData); + } + return data; + } + + // 辅助方法:将课程对象列表转换为数据列表 + private List> transformCoursesToData(List courses) { + List> data = new ArrayList<>(); + for (Course course : courses) { + List rowData = Arrays.asList( + String.valueOf(course.getCourseID()), + course.getCourseName(), + course.getDescription() + ); + data.add(rowData); + } + return data; + } + + // 辅助方法:将成绩对象列表转换为数据列表 + private List> transformScoresToData(List scores) { + List> data = new ArrayList<>(); + for (Score score : scores) { + List rowData = Arrays.asList( + String.valueOf(score.getScoreID()), + String.valueOf(score.getStudent().getId()), + score.getStudent().getName(), + String.valueOf(score.getCourse().getCourseID()), + score.getCourse().getCourseName(), + String.valueOf(score.getScore()) + ); + data.add(rowData); + } + return data; + } +} \ No newline at end of file diff --git a/project2/src/main/resources/spring-security.xml b/project2/src/main/resources/spring-security.xml index 412183f..55b44eb 100644 --- a/project2/src/main/resources/spring-security.xml +++ b/project2/src/main/resources/spring-security.xml @@ -23,6 +23,8 @@ + +
- +
+
+
+ +
+
+
+
+ +
+
+
@@ -166,7 +178,18 @@ - + diff --git a/project2/web/WEB-INF/views/scoreList.jsp b/project2/web/WEB-INF/views/scoreList.jsp index 7987f95..2687973 100644 --- a/project2/web/WEB-INF/views/scoreList.jsp +++ b/project2/web/WEB-INF/views/scoreList.jsp @@ -130,7 +130,20 @@ style="background-image: url('${pageContext.request.contextPath}/resources/img/jason-blackeye-nyL-rzwP-Mk-unsplash.jpg'); margin-top: -58.59px;">
- +
+
+
+ +
+
+
+
+ +
+
+
+
@@ -138,6 +151,8 @@ + + @@ -147,6 +162,8 @@ + + @@ -168,7 +185,18 @@ - + diff --git a/project2/web/WEB-INF/views/studentList.jsp b/project2/web/WEB-INF/views/studentList.jsp index 78401b3..fa83aec 100644 --- a/project2/web/WEB-INF/views/studentList.jsp +++ b/project2/web/WEB-INF/views/studentList.jsp @@ -130,7 +130,19 @@ style="background-image: url('${pageContext.request.contextPath}/resources/img/jason-blackeye-nyL-rzwP-Mk-unsplash.jpg'); margin-top: -58.59px;">
- +
+
+
+ +
+
+
+
+ +
+
+
成绩ID 学号学生姓名课程ID 课程名 成绩
${score.scoreID} ${score.student.id}${score.student.name}${score.course.courseID} ${score.course.courseName} ${score.score}
@@ -172,7 +184,18 @@ - +