sails:如何将excel文件数据读入json格式

时间:2022-11-27 19:36:31

I'm trying to read excel file in local folder after uploading excel file.

我正在尝试在上传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);


                                });
              }

if it is text file means i will read successfully but in the case of excel file, i'cant able to read excel data.

如果它是文本文件意味着我将成功读取但在excel文件的情况下,我无法读取Excel数据。

and also i need to convert this excel file data into json format

我还需要将此excel文件数据转换为json格式

1 个解决方案

#1


2  

try xlsx-rows

simple and easy :)

简单容易:)

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

#1


2  

try xlsx-rows

simple and easy :)

简单容易:)

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