├── .gitignore ├── README.md ├── package.json ├── resources └── static │ └── assets │ └── uploads │ └── 1594085817601-bezkoder-tutorials.xlsx └── src ├── config └── db.config.js ├── controllers └── tutorials │ └── excel.controller.js ├── middlewares └── upload.js ├── models ├── index.js └── tutorial.model.js ├── routes └── tutorial.routes.js └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | package-lock.json -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Node.js: Upload Excel file data into MySQL Database 2 | 3 | For more detail, please visit: 4 | > [Node.js: Upload/Import Excel file data into Database](https://bezkoder.com/node-js-upload-excel-file-database/) 5 | 6 | > [Node.js Download Excel file example with exceljs](https://bezkoder.com/node-js-download-excel-file/) 7 | 8 | More tutorials: 9 | > [Build Node.js Rest APIs with Express, Sequelize & MySQL](https://bezkoder.com/node-js-express-sequelize-mysql/) 10 | 11 | > [Server side Pagination in Node.js with Sequelize and MySQL](https://bezkoder.com/node-js-sequelize-pagination-mysql/) 12 | 13 | > [Deploying/Hosting Node.js app on Heroku with MySQL database](https://bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/) 14 | 15 | Associations: 16 | > [Sequelize Associations: One-to-Many Relationship example](https://bezkoder.com/sequelize-associate-one-to-many/) 17 | 18 | > [Sequelize Associations: Many-to-Many Relationship example](https://bezkoder.com/sequelize-associate-many-to-many/) 19 | 20 | Fullstack: 21 | > [Vue.js + Node.js + Express + MySQL example](https://bezkoder.com/vue-js-node-js-express-mysql-crud-example/) 22 | 23 | > [Vue.js + Node.js + Express + MongoDB example](https://bezkoder.com/vue-node-express-mongodb-mevn-crud/) 24 | 25 | > [Angular + Node.js + Express + MySQL example](https://bezkoder.com/angular-node-express-mysql/) 26 | 27 | > [React + Node.js + Express + MySQL example](https://bezkoder.com/react-node-express-mysql/) 28 | 29 | ## Project setup 30 | ``` 31 | npm install 32 | ``` 33 | 34 | ### Run 35 | ``` 36 | node src/server.js 37 | ``` 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-js-upload-download-excel-files", 3 | "version": "1.0.0", 4 | "description": "Node.js Upload/Import Excel files to MySQL Database and download Excel File", 5 | "main": "src/server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "node js", 11 | "upload", 12 | "import", 13 | "download", 14 | "excel", 15 | "files", 16 | "mysql", 17 | "database" 18 | ], 19 | "author": "bezkoder", 20 | "license": "ISC", 21 | "dependencies": { 22 | "exceljs": "^4.0.1", 23 | "express": "^4.17.1", 24 | "multer": "^1.4.2", 25 | "mysql2": "^2.1.0", 26 | "read-excel-file": "^4.0.6", 27 | "sequelize": "^5.21.13" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /resources/static/assets/uploads/1594085817601-bezkoder-tutorials.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/node-js-upload-excel-file/3297691cbdc71575409b898c46f7ba1fc902e189/resources/static/assets/uploads/1594085817601-bezkoder-tutorials.xlsx -------------------------------------------------------------------------------- /src/config/db.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | HOST: "localhost", 3 | USER: "root", 4 | PASSWORD: "123456", 5 | DB: "testdb", 6 | dialect: "mysql", 7 | pool: { 8 | max: 5, 9 | min: 0, 10 | acquire: 30000, 11 | idle: 10000 12 | } 13 | }; -------------------------------------------------------------------------------- /src/controllers/tutorials/excel.controller.js: -------------------------------------------------------------------------------- 1 | const db = require("../../models"); 2 | const Tutorial = db.tutorials; 3 | 4 | const readXlsxFile = require("read-excel-file/node"); 5 | const excel = require("exceljs"); 6 | 7 | const upload = async (req, res) => { 8 | try { 9 | if (req.file == undefined) { 10 | return res.status(400).send("Please upload an excel file!"); 11 | } 12 | 13 | let path = 14 | __basedir + "/resources/static/assets/uploads/" + req.file.filename; 15 | 16 | readXlsxFile(path).then((rows) => { 17 | // skip header 18 | rows.shift(); 19 | 20 | let tutorials = []; 21 | 22 | rows.forEach((row) => { 23 | let tutorial = { 24 | id: row[0], 25 | title: row[1], 26 | description: row[2], 27 | published: row[3], 28 | }; 29 | 30 | tutorials.push(tutorial); 31 | }); 32 | 33 | Tutorial.bulkCreate(tutorials) 34 | .then(() => { 35 | res.status(200).send({ 36 | message: "Uploaded the file successfully: " + req.file.originalname, 37 | }); 38 | }) 39 | .catch((error) => { 40 | res.status(500).send({ 41 | message: "Fail to import data into database!", 42 | error: error.message, 43 | }); 44 | }); 45 | }); 46 | } catch (error) { 47 | console.log(error); 48 | res.status(500).send({ 49 | message: "Could not upload the file: " + req.file.originalname, 50 | }); 51 | } 52 | }; 53 | 54 | const getTutorials = (req, res) => { 55 | Tutorial.findAll() 56 | .then((data) => { 57 | res.send(data); 58 | }) 59 | .catch((err) => { 60 | res.status(500).send({ 61 | message: 62 | err.message || "Some error occurred while retrieving tutorials.", 63 | }); 64 | }); 65 | }; 66 | 67 | const download = (req, res) => { 68 | Tutorial.findAll().then((objs) => { 69 | let tutorials = []; 70 | 71 | objs.forEach((obj) => { 72 | tutorials.push({ 73 | id: obj.id, 74 | title: obj.title, 75 | description: obj.description, 76 | published: obj.published, 77 | }); 78 | }); 79 | 80 | let workbook = new excel.Workbook(); 81 | let worksheet = workbook.addWorksheet("Tutorials"); 82 | 83 | worksheet.columns = [ 84 | { header: "Id", key: "id", width: 5 }, 85 | { header: "Title", key: "title", width: 25 }, 86 | { header: "Description", key: "description", width: 25 }, 87 | { header: "Published", key: "published", width: 10 }, 88 | ]; 89 | 90 | // Add Array Rows 91 | worksheet.addRows(tutorials); 92 | 93 | res.setHeader( 94 | "Content-Type", 95 | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" 96 | ); 97 | res.setHeader( 98 | "Content-Disposition", 99 | "attachment; filename=" + "tutorials.xlsx" 100 | ); 101 | 102 | return workbook.xlsx.write(res).then(function () { 103 | res.status(200).end(); 104 | }); 105 | }); 106 | }; 107 | 108 | module.exports = { 109 | upload, 110 | getTutorials, 111 | download, 112 | }; 113 | -------------------------------------------------------------------------------- /src/middlewares/upload.js: -------------------------------------------------------------------------------- 1 | const multer = require("multer"); 2 | 3 | const excelFilter = (req, file, cb) => { 4 | if ( 5 | file.mimetype.includes("excel") || 6 | file.mimetype.includes("spreadsheetml") 7 | ) { 8 | cb(null, true); 9 | } else { 10 | cb("Please upload only excel file.", false); 11 | } 12 | }; 13 | 14 | var storage = multer.diskStorage({ 15 | destination: (req, file, cb) => { 16 | cb(null, __basedir + "/resources/static/assets/uploads/"); 17 | }, 18 | filename: (req, file, cb) => { 19 | console.log(file.originalname); 20 | cb(null, `${Date.now()}-bezkoder-${file.originalname}`); 21 | }, 22 | }); 23 | 24 | var uploadFile = multer({ storage: storage, fileFilter: excelFilter }); 25 | module.exports = uploadFile; 26 | -------------------------------------------------------------------------------- /src/models/index.js: -------------------------------------------------------------------------------- 1 | const dbConfig = require("../config/db.config.js"); 2 | 3 | const Sequelize = require("sequelize"); 4 | const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, { 5 | host: dbConfig.HOST, 6 | dialect: dbConfig.dialect, 7 | operatorsAliases: false, 8 | 9 | pool: { 10 | max: dbConfig.pool.max, 11 | min: dbConfig.pool.min, 12 | acquire: dbConfig.pool.acquire, 13 | idle: dbConfig.pool.idle 14 | } 15 | }); 16 | 17 | const db = {}; 18 | 19 | db.Sequelize = Sequelize; 20 | db.sequelize = sequelize; 21 | 22 | db.tutorials = require("./tutorial.model.js")(sequelize, Sequelize); 23 | 24 | module.exports = db; -------------------------------------------------------------------------------- /src/models/tutorial.model.js: -------------------------------------------------------------------------------- 1 | module.exports = (sequelize, Sequelize) => { 2 | const Tutorial = sequelize.define("tutorial", { 3 | title: { 4 | type: Sequelize.STRING 5 | }, 6 | description: { 7 | type: Sequelize.STRING 8 | }, 9 | published: { 10 | type: Sequelize.BOOLEAN 11 | } 12 | }); 13 | 14 | return Tutorial; 15 | }; -------------------------------------------------------------------------------- /src/routes/tutorial.routes.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const router = express.Router(); 3 | const excelController = require("../controllers/tutorials/excel.controller"); 4 | const upload = require("../middlewares/upload"); 5 | 6 | let routes = (app) => { 7 | router.post("/upload", upload.single("file"), excelController.upload); 8 | router.get("/tutorials", excelController.getTutorials); 9 | 10 | router.get("/download", excelController.download); 11 | 12 | app.use("/api/excel", router); 13 | }; 14 | 15 | module.exports = routes; 16 | -------------------------------------------------------------------------------- /src/server.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const app = express(); 3 | const db = require("./models"); 4 | const initRoutes = require("./routes/tutorial.routes"); 5 | 6 | global.__basedir = __dirname + "/.."; 7 | 8 | app.use(express.urlencoded({ extended: true })); 9 | initRoutes(app); 10 | 11 | db.sequelize.sync(); 12 | // db.sequelize.sync({ force: true }).then(() => { 13 | // console.log("Drop and re-sync db."); 14 | // }); 15 | 16 | let port = 8080; 17 | app.listen(port, () => { 18 | console.log(`Running at localhost:${port}`); 19 | }); 20 | --------------------------------------------------------------------------------