简体   繁体   中英

how to import excel file in to mysql using nodejs

我正在尝试将excel文件中的数据导入到mysql中,就像使用nodejs进行行冒号一样,是否有我可以学习的引用或执行我工作的任何nodejs模块或任何示例代码?

I used Npm packages "xlsx-to-json-lc" and "xls-to-json-lc" to import excel file to json directly without converting to csv. Hope this helps...

   var storage = multer.diskStorage({ //multers disk storage settings
            destination: function (req, file, cb) {
                cb(null, './uploads/')
            },
            filename: function (req, file, cb) {

                var datetimestamp = dateFormat(new Date(), "yyyy~mm~dd h~MM~ss");

                cb(null, '`enter code here`templete' + '-' + datetimestamp + '.' + 
                `enter code here`file.originalname.split('.')[file.originalname.split('.').length - 1])
                filename = file.fieldname;


            }
        });

        var upload = multer({ //multer settings
            storage: storage,
            fileFilter: function (req, file, callback) { //file filter
                if (['xls', 'xlsx'].indexOf(file.originalname.split('.')[file.originalname.split('.').length - 1]) === -1) {
                    return callback(new Error('Wrong extension type'));
                }
                callback(null, true);
            }
        }).single('file');

     var exceltojson;
        upload(req, res, function (err) {
            if (err) {
                res.json({ error_code: 1, err_desc: err });
                return;
            }
            if (!req.file) {
                //res.json({ error_code: 1, err_desc: err });
                return;
            }
            if (req.file.originalname.split('.')[req.file.originalname.split('.').length - 1] === 'xlsx') {
                exceltojson = xlsxtojson;
            } else {
                exceltojson = xlstojson;
            }
            try {
                exceltojson({
                    input: req.file.path,
                    output: null, //since we don't need output.json
                    //lowerCaseHeaders: true

                }, function (err, result) {
                    if (err) {
                        return res.json({ error_code: 1, err_desc: err, data: null });
                    }
                    else {
console.log(result);
    }
    });
    })

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