我是angular的新手,有一个来自Angular 6的POST请求的JSON响应,我想将JSON中的数据写入Excel。 JSON的结构如下:
[{
"client_products": [
{
"price": 2,
"part_number": "part1",
"customer_code": "112233",
"products": [
{
"num_sub_products": 1,
"product": "dummy1",
"product_name": "Product1",
},
{
"num_sub_products": 1,
"product": "dummy2",
"product_name": "Product2",
}
],
"quarter": 1,
"volume": 363,
"year": 2018
},
{
"price": 34,
"part_number": "part1",
"customer_code": "112233",
"products": [
{
"num_sub_products": 1,
"product": "dummy1",
"product_name": "Product1",
},
{
"num_sub_products": 1,
"product": "dummy2",
"product_name": "Product2",
}
],
"quarter": 2,
"volume": 6764,
"year": 2018
}
],
},
{
"client_products": [
{
"price": 3,
"part_number": "part2",
"customer_code": "998877",
"products": [
{
"num_sub_products": 1,
"product": "dummy3",
"product_name": "Product3",
}
],
"quarter": 1,
"volume": 5756452,
"year": 2018
},
{
"price": 51,
"part_number": "part2",
"customer_code": "998877",
"products": [
{
"num_sub_products": 1,
"product": "dummy3",
"product_name": "Product3",
}
],
"quarter": 2,
"volume": 43546,
"year": 2018
},
{
"price": 5,
"part_number": "part2",
"customer_code": "998877",
"products": [
{
"num_sub_products": 1,
"product": "dummy3",
"product_name": "Product3",
}
],
"quarter": 3,
"volume": 74534,
"year": 2018
}
]
}]
Excel的结构看起来像所附的图像。 excel structure 我看过不同的第三方程序包,例如xlsx,但它们仅复制正确格式的表,而不复制上面的自定义结构。
答案 0 :(得分:0)
编写CSV文件可能会获得最佳效果。有很多的javascript和angular模块可以帮助您解决这一问题。我尝试直接用javascript编写Excel格式,但结果不尽人意。
就结构而言,Excel(和CSV)格式不支持嵌套数据,因此您必须将代码中的JSON展平,以获取适合于以指定格式写入CSV文件的内容
顺便说一句,问题不仅仅是Angular问题,而是JavaScript问题,因此您可以考虑在问题中添加javascript标签。