Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

581
Views
XLSX Angular export sheet from nested array in one sheet

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

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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);
about 4 years ago · Juan Pablo Isaza Report

0

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

This is for simple format.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!