XLSX Angular Extort从一张纸中的嵌套阵列出口表

发布于 2025-01-27 18:18:31 字数 881 浏览 2 评论 0原文

我正在使用此软件包 xlsx

想要导出excel Sheet示例中添加的对象导出的嵌套阵列下面添加的excel Sheet示例,请帮忙!

let data = [
    {
    "product": "Test Product",
    "id": 1,
    "childProduct": [
        { name: "Child Product 1", "id": 11 },
        { name: "Child Product 2", "id": 12 }
    ]
},
{
    "product": "Test Product",
    "id": 1,
    "childProduct": [
        { "name": "Child Product 1", "id": 13 },
        { "name": "Child Product 2", "id": 14 }
    ]
}
]

我已经尝试过这种解决方案,但是它不起作用,它总是替换最后一个数据。

let wb = xlsx.utils.book_new();
data.map((item: any) => {
  xlsx.utils.json_to_sheet(item.childProduct);
})

单板应该像这样:

I am using this package xlsx

Wanted to export nested array of object export in excel sheet example added below, please help!

let data = [
    {
    "product": "Test Product",
    "id": 1,
    "childProduct": [
        { name: "Child Product 1", "id": 11 },
        { name: "Child Product 2", "id": 12 }
    ]
},
{
    "product": "Test Product",
    "id": 1,
    "childProduct": [
        { "name": "Child Product 1", "id": 13 },
        { "name": "Child Product 2", "id": 14 }
    ]
}
]

I have tried this solutions, but it isn't work it always replace last data.

let wb = xlsx.utils.book_new();
data.map((item: any) => {
  xlsx.utils.json_to_sheet(item.childProduct);
})

Single sheet should be like this:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

活雷疯 2025-02-03 18:18:31
let dummyArray =[]
    data.map((dat)=>{
      dat.childProduct.forEach((x)=>{
        dummyArray.push(x)
      })
    });
xlsx.utils.json_to_sheet(dummyArray)

这是用于简单格式的。

let dummyArray =[]
    data.map((dat)=>{
      dat.childProduct.forEach((x)=>{
        dummyArray.push(x)
      })
    });
xlsx.utils.json_to_sheet(dummyArray)

This is for simple format.

醉生梦死 2025-02-03 18:18:31

我不确定是否有一种更优雅的方法可以使用内置的XLSX库方法来执行此操作,但是您可以转换数据以获取所需的输出。

阵列您将传递到json_to_sheet函数必须像这样构建:

[
  { "name": "Child Product 1", "id": 11 },
  { "name": "Child Product 2", "id": 12 },
  { "name": "--", "id": "--" },
  { "name": "name", "id": "id" },
  { "name": "Child Product 1", "id": 13 },
  { "name": "Child Product 2", "id": 14 }
]

将数据转换为新数组的导出:

const exportData = data.map((item, i) => {
  if (data.length !== i+1) {
    item.childProduct.push({"id": "--", "name": "--"});
    item.childProduct.push({"id": "id", "name": "name"});
  }
  return item.childProduct;
}).flat();

使用库的导出数据:

xlsx.utils.json_to_sheet(exportData);

I am not sure if there is a more elegant way to do this with built-in xlsx library methods, but you can transform data to get desired output.

Array you pass into json_to_sheet function has to be structured like this:

[
  { "name": "Child Product 1", "id": 11 },
  { "name": "Child Product 2", "id": 12 },
  { "name": "--", "id": "--" },
  { "name": "name", "id": "id" },
  { "name": "Child Product 1", "id": 13 },
  { "name": "Child Product 2", "id": 14 }
]

Transform data into new array for export:

const exportData = data.map((item, i) => {
  if (data.length !== i+1) {
    item.childProduct.push({"id": "--", "name": "--"});
    item.childProduct.push({"id": "id", "name": "name"});
  }
  return item.childProduct;
}).flat();

Export data using library:

xlsx.utils.json_to_sheet(exportData);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文