Skip to content

Commit

Permalink
teste3
Browse files Browse the repository at this point in the history
  • Loading branch information
Algusto-RC committed Dec 10, 2023
2 parents 75334b9 + bc0164d commit a1905e0
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
32 changes: 32 additions & 0 deletions backend/controllers/ExtractStudents.js
Original file line number Diff line number Diff line change
@@ -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.' });
}
};
2 changes: 2 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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

Expand Down
10 changes: 10 additions & 0 deletions backend/views/routes/Extract.js
Original file line number Diff line number Diff line change
@@ -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;
47 changes: 47 additions & 0 deletions frontend/src/pages/SendStudents/index.js
Original file line number Diff line number Diff line change
@@ -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 (

<ChakraProvider>
<Flex direction="column" minH="100vh">
<Header />
<Container flex="1">
<input type="file" onChange={handleFileChange} />
<button onClick={handleUpload}>Enviar Arquivo</button>
</Container>
<Footer />
</Flex>
</ChakraProvider>

);
};

export default SendStudent;
4 changes: 4 additions & 0 deletions frontend/src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import ExclusionTrilhas from "../pages/ExclusionTrilhas";
import ExclusionEletivas from "../pages/ExclusionEletivas"
import CreateEletivas from "../pages/CreateEletivas";
import CreateTrilhas from "../pages/CreateTrilhas";
<<<<<<< HEAD
import Recommendations from "../pages/Recommendations";
=======
import SendStudent from "../pages/SendStudents"
>>>>>>> bc0164d30f8969d8af6e0ab86b5dc006a0bdbfb9

const Private = ({ Item }) => {
const { signed } = useAuth();
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"exceljs": "^4.4.0",
"multer": "^1.4.5-lts.1"
}
}

0 comments on commit a1905e0

Please sign in to comment.