简体   繁体   中英

how to import excel file (XLSX) to mysql using nodejs

I am trying to import data in excel file to mysql just like row colon using nodejs are there any references i can learn or any module in nodejs that does my work or any sample code

I have searched in google but i have seen solutions for only mongodb and for python ,

A few methods which come to my mind:

  1. Save the excel sheet(s) you want to import in csv format, and then import them into mysql : cf. How to import CSV file to MySQL table By far the simplest method, but you might run into trouble with quotes and commas and other idiosyncrasies.

  2. use an javascript excel parser to read the excel file and perform directly with code the update in your mysql database : cf. https://github.com/SheetJS/js-xlsx That option is more complex but it allows you to automate the task should you have many files to copy.

1.Upload the csv file from a form.

2.Include your configuration file.

Take a look at the following code.

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var path = require('path');
var http = require('http');
var formidable = require('formidable');
var fs = require('fs');
var csvParser = require('csv-parse');
var cm = require('csv-mysql');
var connection=require('./config');

// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })

app.use(express.static('public'));
app.get('/csvupload.html', function (req, res) {
    res.sendFile(path.resolve("../view/csvupload.html"));
})

app.post('/csvfileupload', urlencodedParser, function (req, res) {

    if (req.url == '/csvfileupload') {
        var form = new formidable.IncomingForm();
        form.parse(req, function (err, fields, files) 
        {
            var oldpath = files.filetoupload.path;
            var newpath = 'C:/xampp/htdocs/nodejs/mvc/csvupload/uploads/' + files.filetoupload.name;
            fs.rename(oldpath, newpath, function (err) {
                if (err) throw err;
                res.write('File uploaded and moved!');
                res.end();
            });
            fs.readFile(oldpath, { encoding: 'utf-8' }, function(err, csvData) {

                if (err) 
                {
                    console.log(err);
                }


                csvParser(csvData, { delimiter: ',' }, function(err, data) {
                    if (err)
                    {
                        console.log(err);
                    } 
                    else 
                    {      
                    console.log(data);    
                        var sql = "INSERT INTO customer (id,name,address,designation) VALUES ?";
                        connection.query(sql, [data], function(err) {
                        if (err) throw err;
                        conn.end();
                        });   

                    }
                });
            });
        });

    } 


}).listen(8080); 

I have developed a plugin that does this. Here is the link to the npm module: https://www.npmjs.com/package/xlsx-mysql

And the github link: https://github.com/Rizwaan-Company/xlsx-mysql

Sample Code:

const XLSXtoMYSQL = require('xlsx-mysql');

var optionsZ = {
    mysql: {
        host: '127.0.0.1',
        user: 'root',
        database: 'test',
        password: 'password',
        port: '3306'
    },
    csv: {
        delimiter: '+'
    }
}
var locationZ = __dirname + '/file.xlsx';
var waitT = 1000;


XLSXtoMYSQL(locationZ,optionsZ,waitT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM