如何在nodejs中将嵌套的JSON转换为Excel
我正在尝试将下面的 JSON 转换为 Excel,我正在使用 XLSX,它正在将我的 JSON 转换为 Excel,但是,dailyPointsArray 的嵌套数组在转换为 Excel 后为空。
尝试过的代码
const XLSX = require("xlsx");
const workSheet = XLSX.utils.json_to_sheet(attendanceData);
const workBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workBook, workSheet, "attendance");
XLSX.write(workBook, { bookType: "xlsx", type: "buffer" });
XLSX.write(workBook, { bookType: "xlsx", type: "binary" });
XLSX.writeFile(workBook,"newExcel.xlsx");
attendanceData:[
{
workerId: '1230',
workerFullName: 'A',
workerDepartment: 'INFORMATION TECHNOLOGY',
workerDesignation: 'ASSISTANT MANAGER',
Location: 'locationA',
dailyPointsArray: [
{
inTime: '-',
Date: '23/03/2022',
outTime: '-',
Points: null,
createdAs: 'ABSENT'
},
{
inTime: '-',
Date: '24/03/2022',
outTime: '-',
Points: null,
createdAs: 'ABSENT'
}
],
total_duration: 0,
total_shift_points: 0
},
{
workerId: '1128',
workerFullName: 'B',
workerDepartment: 'INFORMATION TECHNOLOGY',
workerDesignation: 'MANAGER',
Location: 'LocationA',
dailyPointsArray: [
{
inTime: '-',
Date: '23/03/2022',
outTime: '-',
Points: null,
createdAs: 'ABSENT'
},
{
inTime: '-',
Date: '24/03/2022',
outTime: '-',
Points: null,
createdAs: 'ABSENT'
}
],
total_duration: 17,
total_shift_points: 2
},
]
I am trying to convert the below JSON into excel, I am using XLSX for it, it is converting my JSON to excel but, the nested array of dailyPointsArray is blank after converting into excel.
Tried code
const XLSX = require("xlsx");
const workSheet = XLSX.utils.json_to_sheet(attendanceData);
const workBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workBook, workSheet, "attendance");
XLSX.write(workBook, { bookType: "xlsx", type: "buffer" });
XLSX.write(workBook, { bookType: "xlsx", type: "binary" });
XLSX.writeFile(workBook,"newExcel.xlsx");
attendanceData:[
{
workerId: '1230',
workerFullName: 'A',
workerDepartment: 'INFORMATION TECHNOLOGY',
workerDesignation: 'ASSISTANT MANAGER',
Location: 'locationA',
dailyPointsArray: [
{
inTime: '-',
Date: '23/03/2022',
outTime: '-',
Points: null,
createdAs: 'ABSENT'
},
{
inTime: '-',
Date: '24/03/2022',
outTime: '-',
Points: null,
createdAs: 'ABSENT'
}
],
total_duration: 0,
total_shift_points: 0
},
{
workerId: '1128',
workerFullName: 'B',
workerDepartment: 'INFORMATION TECHNOLOGY',
workerDesignation: 'MANAGER',
Location: 'LocationA',
dailyPointsArray: [
{
inTime: '-',
Date: '23/03/2022',
outTime: '-',
Points: null,
createdAs: 'ABSENT'
},
{
inTime: '-',
Date: '24/03/2022',
outTime: '-',
Points: null,
createdAs: 'ABSENT'
}
],
total_duration: 17,
total_shift_points: 2
},
]
Below is the excel file output
As you can see the column of dailyPointsArray is empty.
I want to my excel file should be like the below image
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试展平数组:过滤嵌套数组,获取所需的键,以便尝试
以下操作:
try flattening the array: filter nested array, get the keys you want, in order you want
try this:
您还可以使用 typeof 运算符
You can also use typeof operator