Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 9, 2023 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/595b68917be4aedd3fe1a3c780cc0eb5 to your computer and use it in GitHub Desktop.
Save code-boxx/595b68917be4aedd3fe1a3c780cc0eb5 to your computer and use it in GitHub Desktop.
NodeJS Import Excel To MySQL

NODEJS IMPORT EXCEL

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

NOTES

  1. Create a test database and import 1-users.sql.
  2. Gist does not allow Excel files. Convert 2-users.csv to 2-users.xlsx on your own.
  3. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Install the required modules npm i mysql xlsx
    • Run the demo node 3-import.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.

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=1;
Jo Doe jo@doe.com
Job Doe job@doe.com
Joe Doe joe@doe.com
Jog Doe jog@doe.com
Joh Doe joh@doe.com
Joi Doe joi@doe.com
Jol Doe jol@doe.com
Jon Doe jon@doe.com
Jou Doe jou@doe.com
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"
});
// (C) OPEN EXCEL FILE - USE FIRST WORKSHEET
var workbook = xlsx.readFile("2-users.xlsx"),
worksheet = workbook.Sheets[workbook.SheetNames[0]],
range = xlsx.utils.decode_range(worksheet["!ref"]);
// (D) IMPORT EXCEL
for (let row=range.s.r; row<=range.e.r; row++) {
// (D1) READ CELLS
let data = [];
for (let col=range.s.c; col<=range.e.c; col++) {
let cell = worksheet[xlsx.utils.encode_cell({r:row, c:col})];
data.push(cell.v);
}
// (D2) INSERT INTO DATABASE
let sql = "INSERT INTO `users` (`user_name`, `user_email`) VALUES (?,?)";
db.query(sql, data, (err, results, fields) => {
if (err) { return console.error(err.message); }
console.log("USER ID:" + results.insertId);
});
}
// (E) DONE - CLOSE DB CONNECTION
db.end();
call npm i mysql xlsx
node 3-import.js
source "npm i mysql xlsx"
node ./3-import.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment