diff --git a/backend/controllers/ExtractStudents.js b/backend/controllers/ExtractStudents.js new file mode 100644 index 00000000..ae43af9c --- /dev/null +++ b/backend/controllers/ExtractStudents.js @@ -0,0 +1,32 @@ +const exceljs = require('exceljs'); + +exports.extractStudents = async (req, res) => { + const arquivo = req.file; + + const workbook = new exceljs.Workbook(); + try { + await workbook.xlsx.load(arquivo.buffer); + const primeiraPlanilha = workbook.worksheets[0]; + + const nomesColunas = primeiraPlanilha.getRow(1).values; + + const dados = []; + + for (let rowIndex = 2; rowIndex <= primeiraPlanilha.rowCount; rowIndex++) { + const linha = primeiraPlanilha.getRow(rowIndex); + const dadosLinha = {}; + nomesColunas.forEach((nomeColuna, columnIndex) => { + dadosLinha[nomeColuna] = linha.getCell(columnIndex + 1).value; + }); + dados.push(dadosLinha); + } + + + console.log('Dados extraídos:', dados); + + res.json({ mensagem: 'Arquivo Excel recebido e processado com sucesso!' }); + } catch (erro) { + console.error('Erro ao processar arquivo Excel:', erro); + res.status(500).json({ erro: 'Erro ao processar arquivo Excel.' }); + } +}; diff --git a/backend/index.js b/backend/index.js index 75aae8eb..e271e046 100644 --- a/backend/index.js +++ b/backend/index.js @@ -7,6 +7,7 @@ const learningPathRoute = require('./views/routes/LearningPaths') const logoutRoutes = require('./views/routes/Users'); const sequelize = require('sequelize'); const deleteLearningPathsRoute = require('./views/routes/LearningPaths'); +const extractStudentsRoutes = require('./views/routes/Extract'); require("dotenv").config(); const app = express(); @@ -18,6 +19,7 @@ app.use('/auth', userRoute); app.use('/elective', electiveRoute); app.use('/learningpath', learningPathRoute); app.use('/api', logoutRoutes); +app.use('/send-file', extractStudentsRoutes); let test = process.env.DB_USERNAME diff --git a/backend/views/routes/Extract.js b/backend/views/routes/Extract.js new file mode 100644 index 00000000..54371438 --- /dev/null +++ b/backend/views/routes/Extract.js @@ -0,0 +1,10 @@ +const express = require('express'); +const multer = require('multer'); +const router = express.Router(); +const extractController = require('../../controllers/ExtractStudents') + +const storage = multer.memoryStorage(); +const upload = multer({ storage: storage }); + +router.post("/extract-students", upload.single('arquivo'), extractController.extractStudents); +module.exports = router; \ No newline at end of file diff --git a/frontend/src/pages/SendStudents/index.js b/frontend/src/pages/SendStudents/index.js new file mode 100644 index 00000000..f982d9f6 --- /dev/null +++ b/frontend/src/pages/SendStudents/index.js @@ -0,0 +1,47 @@ +import React, { useState } from 'react'; +import axios from 'axios'; +import { Container, Flex} from "@chakra-ui/react"; +import Header from "../../components/Header/index.js"; +import Footer from "../../components/Footer/index.js"; +import { ChakraProvider } from "@chakra-ui/react"; + +import { Link } from "react-router-dom"; + +const SendStudent = () => { + + const [selectedFile, setSelectedFile] = useState(null); + + const handleFileChange = (event) => { + setSelectedFile(event.target.files[0]); + } + + const handleUpload = () => { + const formData = new FormData(); + formData.append('arquivo', selectedFile); + + axios.post('http://localhost:3001/send-file/extract-students', formData) + .then(response => { + console.log('Resposta do servidor:', response.data); + }) + .catch(error => { + console.error('Erro ao enviar arquivo:', error); + }) + } + + return ( + + + +
+ + + + +