sails:How 我可以将 excel 文件数据读入 json 格式吗

sails:How can i read excel file data into json format

上传 excel 文件后,我正在尝试读取本地文件夹中的 excel 文件。

var fs = require('fs');
    uploadFile.upload
             ({
                    // don't allow the total upload size to exceed ~10MB
                    maxBytes: 10000000, saveAs: function(uploadFile, cb) {cb(null,Date.now()+uploadFile.filename ); },dirname: '../../assets/uploads'
                },function whenDone(err, uploadedFiles) {
                if (err) 
                {
                    console.log("error");
                    return res.negotiate(err);
                }
                // If no files were uploaded, respond with an error.
                if (uploadedFiles.length === 0)
                {
                    return res.badRequest('No file was uploaded');
                }
               else
              {
                var fd = uploadedFiles[0].fd;
                                console.log(fd);
                                fs.readFile(fd, 'utf8', function (err, data) 
                                {
                                        console.log(data);


                                });
              }

如果是文本文件意味着我会成功读取,但如果是 excel 文件,我无法读取 excel 数据。

我还需要将这个 excel 文件数据转换成 json 格式

尝试xlsx-rows

简单易行:)

var fd = uploadedFiles[0].fd;
var rows = require('xlsx-rows')(fd);
rows = rows.slice(1);
rows.forEach(function(obj){
    // do something....
});