使用 js-xlsx 将 JSON 输出到多行
Output JSON to multiple lines with js-xlsx
我遵循 中的解决方案之一。该解决方案很好地注销到控制台,但我不知道如何保存,所以它看起来很容易读取到 .json 文件。我尝试使用文件系统模块将输出记录到 .json。我不知道如何让它打印成多行。它全部在一行中出现。
var fs = require('fs');
var XLSX = require('xlsx');
var workbook = XLSX.readFile('test.xlsx');
var sheet_name_list = workbook.SheetNames;
sheet_name_list.forEach(function(y) {
var worksheet = workbook.Sheets[y];
var headers = {};
var data = [];
for(z in worksheet) {
if(z[0] === '!') continue;
//parse out the column, row, and value
var col = z.substring(0,1);
var row = parseInt(z.substring(1));
var value = worksheet[z].v;
//store header names
if(row == 1) {
headers[col] = value;
continue;
}
if(!data[row]) data[row]={};
data[row][headers[col]] = value;
}
//drop those first two rows which are empty
data.shift();
data.shift();
//console.log(data);
fs.writeFileSync("new.json", JSON.stringify(data), function(err) {
if(err) {
return console.log(err);
}
});
});
new.json
的输出
[{"key":"AprilA45","Food":"Sandwich","yr":2017,"mo":"April" ...
使用Array.map和Array.join。
// Get end of line character
const EOL = require('os').EOL;
// Stringify each object in array and join the array with newline character
data = data.map(obj => JSON.stringify(obj)).join(EOL);
// Write your data to file
fs.writeFileSync(...);
您可以通过使用 stringify
方法的附加参数来实现此目的,特别是 space
参数:
A String or Number object that's used to insert white space into the output JSON string for readability purposes. If this is a Number, it indicates the number of space characters to use as white space; this number is capped at 10 (if it is greater, the value is just 10). Values less than 1 indicate that no space should be used. If this is a String, the string (or the first 10 characters of the string, if it's longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used.
这是一个基于浏览器的示例,它显示了您想要的内容 - 首先 div 是您当前获得的内容;第二个 div 就是你想要的:
let data = {"foo": ["bar", "baz"], "qux": "quux"};
document.getElementById("first").innerHTML = "<pre>" + JSON.stringify(data) + "</pre>";
document.getElementById("second").innerHTML = "<pre>" + JSON.stringify(data, null, 2) + "</pre>";
<div id="first"></div>
<div id="second"></div>
因此您的代码将是:
fs.writeFileSync("new.json", JSON.stringify(data, null, 2), function(err) {
if(err) {
return console.log(err);
}
});
我遵循
var fs = require('fs');
var XLSX = require('xlsx');
var workbook = XLSX.readFile('test.xlsx');
var sheet_name_list = workbook.SheetNames;
sheet_name_list.forEach(function(y) {
var worksheet = workbook.Sheets[y];
var headers = {};
var data = [];
for(z in worksheet) {
if(z[0] === '!') continue;
//parse out the column, row, and value
var col = z.substring(0,1);
var row = parseInt(z.substring(1));
var value = worksheet[z].v;
//store header names
if(row == 1) {
headers[col] = value;
continue;
}
if(!data[row]) data[row]={};
data[row][headers[col]] = value;
}
//drop those first two rows which are empty
data.shift();
data.shift();
//console.log(data);
fs.writeFileSync("new.json", JSON.stringify(data), function(err) {
if(err) {
return console.log(err);
}
});
});
new.json
的输出[{"key":"AprilA45","Food":"Sandwich","yr":2017,"mo":"April" ...
使用Array.map和Array.join。
// Get end of line character
const EOL = require('os').EOL;
// Stringify each object in array and join the array with newline character
data = data.map(obj => JSON.stringify(obj)).join(EOL);
// Write your data to file
fs.writeFileSync(...);
您可以通过使用 stringify
方法的附加参数来实现此目的,特别是 space
参数:
A String or Number object that's used to insert white space into the output JSON string for readability purposes. If this is a Number, it indicates the number of space characters to use as white space; this number is capped at 10 (if it is greater, the value is just 10). Values less than 1 indicate that no space should be used. If this is a String, the string (or the first 10 characters of the string, if it's longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used.
这是一个基于浏览器的示例,它显示了您想要的内容 - 首先 div 是您当前获得的内容;第二个 div 就是你想要的:
let data = {"foo": ["bar", "baz"], "qux": "quux"};
document.getElementById("first").innerHTML = "<pre>" + JSON.stringify(data) + "</pre>";
document.getElementById("second").innerHTML = "<pre>" + JSON.stringify(data, null, 2) + "</pre>";
<div id="first"></div>
<div id="second"></div>
因此您的代码将是:
fs.writeFileSync("new.json", JSON.stringify(data, null, 2), function(err) {
if(err) {
return console.log(err);
}
});