【问题标题】:Can't get xlsx to JSON converter to work properly in Node/Express无法让 xlsx 到 JSON 转换器在 Node/Express 中正常工作
【发布时间】:2016-11-03 15:29:19
【问题描述】:

我正在使用下面的包尝试在我的 Express Web 应用程序上将上传的 excel 文件 (.xlsx) 转换为 JSON 文件:

https://www.npmjs.com/package/xlsx-to-json

这是我供用户上传的表单:

        form(id = "form1", action="/upload", method="post", enctype="multipart/form-data")
          input(type="file", id="control", name="XLupload")
          br
          input(type="submit" value="Upload" name="Submit")

这是我在我的主要快递 (app.js) 文件中上传回的路由:

var multer = require('multer');
var upload = multer({dest: './uploads'});
var excel_upload = upload.single('XLupload');

app.post('/upload', excel_upload, function(req, res) {

var fileObject = req.file;
var filePath = fileObject.path;
/*** This is what the file Object looks like when uploaded:
{ fieldname: 'XLupload',
  originalname: 'testing.xlsx',
  encoding: '7bit',
  mimetype: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',

  destination: './uploads',
  filename: 'c1d55ea7d1f6fccc7e3d3d2764db8881',
  path: 'uploads\\c1d55ea7d1f6fccc7e3d3d2764db8881',
  size: 8013 }
***/

xlsxj({
    input: String(filePath),
    output: "output.json"
    }, function(err, result) {
        if (err) {
            console.log(err);
        } else {
            console.log(result);
            }
        });
});

无论如何,简而言之,上传似乎工作正常,即它们被上传到目录中的 /uploads 文件夹。但是,我从 xlsxj 转换器返回的 JSON 文件是空的,我不知道为什么。我用随机单元格中的一些单词制作了一个小的测试 xlsx 文件,但它仍然让我回想一个空的 [] 在 output.json 中。任何人都可以让我知道我做错了什么?

【问题讨论】:

    标签: json node.js express xlsx multer


    【解决方案1】:

    您可以尝试使用此库 XLSX (https://github.com/SheetJS/js-xlsx) 并在获取工作表后添加此代码

    var roa = XLSX.utils.sheet_to_row_object_array(worksheet);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-22
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      • 2020-03-22
      • 1970-01-01
      相关资源
      最近更新 更多