feat:新增导出与模板功能,另:现在可以兼容excel

backend
poppoppuppylove 2 months ago
parent 52ca2b385a
commit d26d8eaec7

@ -3,11 +3,20 @@ package com.example.attendance.controller;
import com.example.attendance.entity.PointsRequest; import com.example.attendance.entity.PointsRequest;
import com.example.attendance.entity.Student; import com.example.attendance.entity.Student;
import com.example.attendance.service.StudentService; 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.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List; import java.util.List;
@RestController @RestController
@ -105,4 +114,55 @@ public class StudentController {
return "学生数据导入失败:" + e.getMessage(); return "学生数据导入失败:" + e.getMessage();
} }
} }
/*
*/
@GetMapping("/export-students")
public ResponseEntity<byte[]> exportStudents() throws IOException {
List<Student> 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<String> getTemplateDownloadLink() {
// 返回百度网盘的模板下载链接
String downloadLink = "https://pan.baidu.com/s/1NUukdPo4qUVbM4V9MWTx2g?pwd=1234";
return ResponseEntity.ok(downloadLink);
}
} }

@ -3,11 +3,13 @@ package com.example.attendance.service;
import com.example.attendance.entity.Student; import com.example.attendance.entity.Student;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.OutputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.List; import java.util.List;
public interface StudentService { public interface StudentService {
void importStudents(MultipartFile file) throws Exception ; void importStudents(MultipartFile file) throws Exception ;
void exportStudents(OutputStream outputStream) throws Exception;
void adjustPoints(String studentNumber, BigDecimal pointsDelta); void adjustPoints(String studentNumber, BigDecimal pointsDelta);
List<Student> getStudentRanking(int page, int size); List<Student> getStudentRanking(int page, int size);
Student findById(Long id); Student findById(Long id);

@ -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

Loading…
Cancel
Save