如何阅读excel表并在节点js中上传MSSQL服务器

时间:2022-06-01 18:38:51

I want to upload excel sheet and after submit that excel sheet need to insert data into MSSQL database and same sheet which we upload need to download.

我想上传excel表格并在提交excel表格后需要将数据插入MSSQL数据库和我们上传的同一张表需要下载。

1 个解决方案

#1


1  

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...

我使用Npm包“xlsx-to-json-lc”和“xls-to-json-lc”将excel文件直接导入json而不转换为csv。希望这可以帮助...

 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);
    }
    });
    })

#1


1  

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...

我使用Npm包“xlsx-to-json-lc”和“xls-to-json-lc”将excel文件直接导入json而不转换为csv。希望这可以帮助...

 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);
    }
    });
    })