我正在尝试将 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。 试过:
通过了工作表:“登录”,“注销”和工作表:“登录,注销”。
创建一个 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/