Upload & Read Excel File in Node.js
Tags: node, excel, javascript, webdev
Published: 2021-01-06 04:31:40

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/](https://bezkoder.com/node-js-upload-excel-file-database/)

<h2>Node.js Upload & read Excel File Rest APIs</h2>
Assume that we have an <strong>.xlsx</strong> file that contains Tutorial data in <strong>Tutorials</strong> sheet as following:

![upload-read-excel-file-node-js-excel-data](https://dev-to-uploads.s3.amazonaws.com/i/v6gxve9ex3j5xa46o36k.png)

We're gonna create a Node.js Application that provides APIs for:
<ul>
	<li>uploading Excel File to the Node.js Express Server & storing data in MySQL Database</li>
	<li>getting list of items from MySQL table</li>
	<li>downloading MySQL table data as Excel file</li>
</ul>

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](https://dev-to-uploads.s3.amazonaws.com/i/p4cec8w05ya9wwtqw48p.png)

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

![upload-read-excel-file-node-js-retrieve-data](https://dev-to-uploads.s3.amazonaws.com/i/hq5xlha8gx0pr6rzt8lc.png)

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

<h2>Node.js Rest API that exports Excel File</h2>
If you send request to <code>/api/excel/download</code>, the server will return a response with an Excel file <strong>tutorials.xlsx</strong> that contains data in MySQL table:

![upload-read-excel-file-node-js-download-file](https://dev-to-uploads.s3.amazonaws.com/i/zbznv3c3vewqaml1m2ep.png)

How to do this?
You need to set the HTTP header:
<pre><code class="language-bash">"Content-disposition" : "attachment; filename=[yourFileName]" 
"Content-Type" : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
</code></pre>

You can find step by step for downloading Excel file in the tutorial:
<a href="https://bezkoder.com/node-js-download-excel-file/">Node.js Download Excel file example with exceljs</a>

These are APIs to be exported:
<div class="table-responsive">
<table class="table table-hover  table table-hover" width="100%">
<thead>
<tr>
<th>Methods</th>
<th>Urls</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>POST</td>
<td>/api/excel/upload</td>
<td>upload an Excel File</td>
</tr>
<tr>
<td>GET</td>
<td>/api/excel/tutorials</td>
<td>get List of items in db table</td>
</tr>
<tr>
<td>GET</td>
<td>/api/excel/download</td>
<td>download db data as Excel file</td>
</tr>
</tbody>
</table>
</div>

## Further Reading
If you want to add Pagination while getting data from MySQL table, you can find the instruction at:
<a href="https://bezkoder.com/node-js-sequelize-pagination-mysql/">Server side Pagination in Node.js with Sequelize & MySQL</a>

Or working with CSV file instead:
<a href="https://bezkoder.com/node-js-upload-csv-file-database/">Node.js: Upload CSV file data into Database with Express</a>

Fullstack:
- <a href="https://bezkoder.com/vue-js-node-js-express-mysql-crud-example/">Vue.js + Node.js + Express + MySQL example</a>
- <a href="https://bezkoder.com/vue-node-express-mongodb-mevn-crud/">Vue.js + Node.js + Express + MongoDB example</a>
- <a href="https://bezkoder.com/angular-node-express-mysql/">Angular 8 + Node.js Express + MySQL example</a>
- <a href="https://bezkoder.com/angular-10-node-js-express-mysql/">Angular 10 + Node.js Express + MySQL example</a>
- <a href="https://bezkoder.com/angular-11-node-js-express-mysql/">Angular 11 + Node.js Express + MySQL example</a>
- <a href="https://bezkoder.com/angular-12-node-js-express-mysql/">Angular 12 + Node.js Express + MySQL example</a>
- <a href="https://bezkoder.com/react-node-express-mysql/">React + Node.js + Express + MySQL example</a>

Security: <a href="https://bezkoder.com/node-js-jwt-authentication-mysql/">Node.js – JWT Authentication & Authorization example</a>

Deployment:
- <a href="https://bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/">Deploying/Hosting Node.js app on Heroku with MySQL database</a>
- [Dockerize Node.js Express and MySQL example – Docker Compose](https://www.bezkoder.com/docker-compose-nodejs-mysql/)

Node.js & MySQL Associations:
- <a href="https://bezkoder.com/sequelize-associate-one-to-many/">One-to-Many Relationship example</a>
- <a href="https://bezkoder.com/sequelize-associate-many-to-many/">Many-to-Many Relationship example</a>