我一直在尝试从包含 JSON 数据和大量数组的 Tree Family 结构中导出数据。
导出功能工作得很好,但它并没有导出整个东西。
这是我用于使用 npm-xlsx 导出功能的代码。
//excel button click functionality
exportExcel() {
import('xlsx').then((xlsx) => {
const ws: XLSX.WorkSheet = XLSX.utils.json_to_sheet(this.dataSource.data);
const wb: XLSX.WorkBook = { Sheets: { data: ws }, SheetNames: ['data'] };
const excelBuffer: any = XLSX.write(wb, {
bookType: 'xlsx',
type: 'array',
});
this.saveAsExcelFile(excelBuffer, 'treeData');
});
}
saveAsExcelFile(buffer: any, fileName: string): void {
let EXCEL_TYPE =
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
let EXCEL_EXTENSION = '.xlsx';
const data: Blob = new Blob([buffer], {
type: EXCEL_TYPE,
});
saveAs(
data,
fileName + '_export_' + new Date().getTime() + EXCEL_EXTENSION
);
}
这是来自 Tree Family 结构的 JSON 数据。(this.dataSource.data)
[
{
"name": "Folder 1",
"id": 1,
"numbering": "1.0",
"children": [
{
"name": "Folder 1.1",
"id": 2,
"numbering": "1.1",
"children": [
{
"name": "Folder 1.1.1",
"id": 3,
"numbering": "1.1.1",
"children": []
},
{
"name": "File 1.1.2",
"id": 33,
"numbering": "1.1.2",
"children": [
{
"name": "Folder 1.1.2.1",
"id": 4,
"numbering": "1.1.2.1"
},
{
"name": "Folder 1.1.2.2",
"id": 5,
"numbering": "1.1.2.2"
},
{
"name": "Folder 1.1.2.3",
"id": 6,
"numbering": "1.1.2.3",
"children": [
{
"name": "Folder 1.1.2.3.1",
"id": 7,
"numbering": "1.1.2.3.1"
},
{
"name": "Folder 1.1.2.3.2",
"id": 8,
"numbering": "1.1.2.3.2"
}
]
},
{
"name": "Folder 1.1.2.4",
"id": 9,
"numbering": "1.1.2.4",
"children": [
{
"name": "Folder 1.1.2.4.1",
"id": 10,
"numbering": "1.1.2.4.1"
}
]
}
]
},
{
"name": "File 1.1.3",
"id": 11,
"numbering": "1.1.3",
"children": [
{
"name": "Folder 1.1.3.1",
"id": 12,
"numbering": "1.1.3.1",
"children": [
{
"name": "Folder 1.1.3.1.1",
"id": 13,
"numbering": "1.1.3.1.1"
},
{
"name": "Folder 1.1.3.1.2",
"id": 14,
"numbering": "1.1.3.1.2"
},
{
"name": "Folder 1.1.3.1.3",
"id": 15,
"numbering": "1.1.3.1.3"
},
{
"name": "Folder 1.1.3.1.4",
"id": 16,
"numbering": "1.1.3.1.4",
"children": [
{
"name": "Folder 1.1.3.1.5",
"id": 17,
"numbering": "1.1.3.1.5"
}
]
}
]
}
]
}
]
}
]
}
]
它没有导出整个东西。它在 JSON 中的第一个子数据处停止。也许是因为它包含数据数组?