DEV Community

loading...
Cover image for Upload & Read Excel File in Node.js

Upload & Read Excel File in Node.js

tienbku profile image Tien Nguyen Originally published at bezkoder.com Updated on ・2 min read

The Excel file is a spreadsheet file format created by Microsoft for use with Microsoft Excel. You can use the file to create, view, edit, analyse data, charts, budgets and more. In this tutorial, I will show you how to upload & read Excel file in Node.js then save data into MySQL Database.

Full Article: https://bezkoder.com/node-js-upload-excel-file-database/

Node.js Upload & read Excel File Rest APIs

Assume that we have an .xlsx file that contains Tutorial data in Tutorials sheet as following:

upload-read-excel-file-node-js-excel-data

We're gonna create a Node.js Application that provides APIs for:

  • uploading Excel File to the Node.js Express Server & storing data in MySQL Database
  • getting list of items from MySQL table
  • downloading MySQL table data as Excel file

After the Excel file is uploaded and read successfully, tutorials table in MySQL database will look like this:

upload-read-excel-file-node-js-database-table

If we get list of Tutorials, the Node.js Rest Apis will return:

upload-read-excel-file-node-js-retrieve-data

For more details, implementation & github, please visit:
https://bezkoder.com/node-js-upload-excel-file-database/

Node.js Rest API that exports Excel File

If you send request to /api/excel/download, the server will return a response with an Excel file tutorials.xlsx that contains data in MySQL table:

upload-read-excel-file-node-js-download-file

How to do this?
You need to set the HTTP header:

"Content-disposition" : "attachment; filename=[yourFileName]" 
"Content-Type" : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

You can find step by step for downloading Excel file in the tutorial:
Node.js Download Excel file example with exceljs

These are APIs to be exported:

Methods Urls Actions
POST /api/excel/upload upload an Excel File
GET /api/excel/tutorials get List of items in db table
GET /api/excel/download download db data as Excel file

Further Reading

If you want to add Pagination while getting data from MySQL table, you can find the instruction at:
Server side Pagination in Node.js with Sequelize & MySQL

Or working with CSV file instead:
Node.js: Upload CSV file data into Database with Express

Discussion (0)

pic
Editor guide