【问题标题】:Convert all sheets in excel to Json with node.js使用 node.js 将 excel 中的所有工作表转换为 Json
【发布时间】:2017-10-08 16:44:50
【问题描述】:

我正在尝试将 excel 转换为 json。

尝试使用“xls-to-json”npm 包。

   node_xj = require("xls-to-json");
node_xj({
input: "Auto.xlsx",  // input xls 
output: "output.json", // output json 
sheet: "Login"  // specific sheetname 
}, function (err, result) {
if (err) {
    console.error(err);
} else {
    console.log(result);
}
});

上面的代码可以很好地将登录表中的所有数据写入 output.json 文件。 我无法弄清楚我应该做哪些更改才能读取 excel 中的所有工作表并将其转换为一个 Json。

或者将值传递给 output: "output.json" 和 sheet: "Login" 动态,以便将每个工作表数据写入不同的 Json。 试过了:

  1. 通过工作表:“Login”、“Logout”和工作表:“Login,Logout”。

  2. 创建一个 Json 文件,从中读取值并将其传递给

    input: jsonobj.input, // 输入 xls

    output: jsonobj.output , // 输出 json

    sheet: jsonobj.sheet // 具体的sheetname

这会读取第一个值,然后抛出错误。

【问题讨论】:

    标签: javascript json node.js npm


    【解决方案1】:

    另一个 excel 到 json 包“convert-excel-to-json”帮助我实现了我的目标。

    var excelToJson = require('convert-excel-to-json');
    
    var result = excelToJson({
    sourceFile: 'Auto.xlsx',
    header: {
    
        rows: 1 
    },
    columnToKey: {
       '*': '{{columnHeader}}'
    }
    }); 
    

    上面的代码给出了 JSON 对象。然后使用包“jsonfile”将其写入一个json文件。

    var jsonfile = require('jsonfile')
    
    var file = 'output.json'
    
    jsonfile.writeFile(file, result, function (err) {
    console.error(err)
    })
    

    【讨论】:

      【解决方案2】:

      它正在删除空白值字段。这是个问题。

      【讨论】:

      • 给出一些更具描述性的答案。
      猜你喜欢
      • 2019-10-26
      • 2020-02-20
      • 2019-02-20
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      相关资源
      最近更新 更多