You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1.1 KiB

const fs = require('fs');
const path = require('path');
const xlsx = require('xlsx'); // 如果需要处理 Excel 文件,需要安装相关库
const pool = require('../db');
const classManager = {
addClass: (req, res) => {
const filePath = req.file.path; // 上传文件的路径
// 读取并解析 Excel 文件
const workbook = xlsx.readFile(filePath);
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
const students = xlsx.utils.sheet_to_json(worksheet); // 将 Excel 转为 JSON 格式
// 假设每个学生的记录中包含 name 和 studentId
students.forEach(student => {
// 将学生数据插入数据库
const query = 'INSERT INTO students (name, student_id, score, call_count) VALUES (?, ?, 0, 0)';
pool.query(query, [student.name, student.studentId], (error) => {
if (error) {
console.error(`Error inserting student: ${student.name}`, error);
}
});
});
res.status(200).json({ message: 'Class added successfully!' });
}
};
module.exports = classManager;