Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 8, 2023 15:05
Show Gist options
  • Save code-boxx/20b17137651ce5e6c3496a842baebd44 to your computer and use it in GitHub Desktop.
Save code-boxx/20b17137651ce5e6c3496a842baebd44 to your computer and use it in GitHub Desktop.
NodeJS Export Database To Excel

NODEJS EXPORT EXCEL

https://code-boxx.com/export-mysql-excel-nodejs/

NOTES

  1. Create a test database and import 1-users.sql.
  2. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Install the required modules npm i mysql xlsx
    • Run the demo node 2-export.js

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-- (A) USERS TABLE
CREATE TABLE `users` (
`user_id` bigint(20) NOT NULL,
`user_name` varchar(255) NOT NULL,
`user_email` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `users`
ADD PRIMARY KEY (`user_id`),
ADD UNIQUE KEY `user_email` (`user_email`),
ADD KEY `user_name` (`user_name`);
ALTER TABLE `users`
MODIFY `user_id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;
-- (B) DUMMY USERS
INSERT INTO `users` (`user_id`, `user_name`, `user_email`) VALUES
(1, 'Jo Doe', 'jo@doe.com'),
(2, 'Job Doe', 'job@doe.com'),
(3, 'Joe Doe', 'joe@doe.com'),
(4, 'Jog Doe', 'jog@doe.com'),
(5, 'Joh Doe', 'joh@doe.com'),
(6, 'Joi Doe', 'joi@doe.com'),
(7, 'Jol Doe', 'jol@doe.com'),
(8, 'Jon Doe', 'jon@doe.com'),
(9, 'Jou Doe', 'jou@doe.com'),
(10, 'Joy Doe', 'joy@doe.com');
// (A) LOAD MODULES
// npm install mysql xlsx
// https://www.npmjs.com/package/mysql
// https://www.npmjs.com/package/xlsx
const mysql = require("mysql"),
xlsx = require("xlsx");
// (B) CONNECT TO DATABASE - CHANGE SETTINGS TO YOUR OWN!
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "test"
});
db.connect();
// (C) EXPORT TO EXCEL
db.query("SELECT * FROM `users`", (error, results) => {
// (C1) EXTRACT DATA FROM DATABASE
if (error) throw error;
var data = [];
results.forEach(row => {
data.push([row["user_name"], row["user_email"]]);
});
// (C2) WRITE TO EXCEL FILE
var worksheet = xlsx.utils.aoa_to_sheet(data),
workbook = xlsx.utils.book_new();
xlsx.utils.book_append_sheet(workbook, worksheet, "Users");
xlsx.writeFile(workbook, "demo.xlsx");
});
// (D) DONE - CLOSE DB CONNECTION
db.end();
call npm i mysql xlsx
node 2-export.js
source "npm i mysql xlsx"
node ./2-export.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment