javascript - 使用 node.js 将 excel 中的所有工作表转换为 Json

标签 javascript json node.js npm

我正在尝试将 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"动态传递,这样每个 sheet 数据都被写入不同的 Json。 试过:

  1. 通过了工作表:“登录”,“注销”和工作表:“登录,注销”。

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

    input: jsonobj.input,//输入xls

    output: jsonobj.output ,//输出json

    sheet: jsonobj.sheet//具体工作表名称

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

最佳答案

另一个 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)
})

关于javascript - 使用 node.js 将 excel 中的所有工作表转换为 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43885624/

相关文章:

javascript - js 中的 Highchart 在 Vue.js 中不起作用

javascript - 解析云查询错误

javascript - Electron - 如何将 base64 dataUrl 保存到文件

javascript - ssh2 模块静默失败,凭据从 CLI 成功

c# - 即时插入/编辑

javascript - 如何在 jquery 中附加到 html 之前编译字符串(包含 Angular 目录)?

java - 使用JERSEY和JACKSON解析未知结构的POST请求

regex - 如何在swift中使用正则表达式?

java - JsonSerializer 不适用于使用 GSON 的嵌套对象

node.js - 使用 app.set ('app_secret' , data) 在nodejs中存储 key 是否安全