Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

582
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda