json - 无法让 xlsx 到 JSON 转换器在 Node/Express 中正常工作

标签 json node.js express xlsx multer

我正在使用下面的包尝试在我的 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")

这是我在主 Express (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 文件,在随机单元格中包含一些单词,但它仍然让我返回一个空的 [] 在输出.json 中。任何人都可以让我知道我做错了什么?

最佳答案

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

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

关于json - 无法让 xlsx 到 JSON 转换器在 Node/Express 中正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31602483/

相关文章:

c# - 将 JSON 字符串反序列化为 C# 复杂类对象

json - Go JSON 解码很慢。什么是更好的方法呢?

json - Golang 优雅地解码不同的 JSON 结构

node.js - Express 中是否可以同时处理两个或多个请求?

node.js - Hapijs - 服务器重新启动时 HttpOnly cookie 消失

javascript - pretty-print 时避免 Jade 模板输出中标签之间的空格

node.js - 如何在 GraphQL 模式中使用 'Decimal' 作为字段类型?

javascript - JSON 填充 View 的暂存区

node.js - react 和表达应用程序不允许jsx

javascript - nodemailer 在 Aws Ec2 上的生产环境中导致错误