-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |