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

448
Visualizações
How to dynamically populate an Excel file using exceljs in express?

Right now I have the following function:

const generateXLSX =  (res, data) => {
    let baseFile = './src/utils/boop.xlsx';
    let wb = new Excel.Workbook();
    wb.xlsx.readFile(baseFile)

    .then (async () => {
        let ws = wb.getWorksheet(1);
        
        let row = ws.getRow(9);
        row.getCell(3).value = 'Simple and not so funny test';
        row.commit();
        res.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        await wb.xlsx.write(res);
        res.end();
        })
};

This will edit my base Excel document and produce this:

Edited Excel via exceljs

The problem here is that I want to populate this template using a JSON Object like the following:

        "id": 1,
        "urlImagen": "http://placeimg.com/640/480",
        "name": "test national",
        "pdu": "53014",
        "creationDate": 2020,
        "appevel": "ascending",
        "ddlevel": "descending",
        "mapa": 1,
        "Module": "Lead",
        "sector": "Something"

As you can see it contains data that i don't want to render into the Excel. I want to implement a dynamic way to allocate the information without writing the same code like:

let row = ws.getRow(9);
row.getCell(3).value = 'Simple and not so funny test';
let row = ws.getRow(10);
row.getCell(3).value = 'Value 2';
let row = ws.getRow(11);
row.getCell(3).value = 'Value 3';

And so on, but I don't know how to implement an optimal way to solve this...

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you need to loop the data and write it into desired cells.

try this

const generateXLSX = async(res, data) => {

    const baseFile = './src/utils/boop.xlsx';

    const wb = new Excel.Workbook();

    await wb.xlsx.readFile(baseFile);

    const ws = wb.getWorksheet(1);

    // loop and write data
    for (const [rowNum, inputData] of data.entries()) {

        console.log('row: ', rowNum, ', data', inputData);

        // increment rowNum to change the row start position if needed
        // for example, start at 5th row:
        // const row = ws.getRow(rowNum+6);
        const row = ws.getRow(rowNum + 1);

        // insert values
        row.getCell(1).value = inputData.pdu;
        row.getCell(2).value = inputData.name;
        row.getCell(3).value = inputData.appevel;
        
        row.commit();
    }

    const fileName = 'excel.xlsx';

    res.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    res.header("Content-Disposition", "attachment; filename=" + fileName);

    await wb.xlsx.write(res);

    res.end();
};

// data format
const data = [{
    "id": 1,
    "urlImagen": "http://placeimg.com/640/480",
    "name": "test national",
    "pdu": "53014",
    "creationDate": 2020,
    "appevel": "ascending",
    "ddlevel": "descending",
    "mapa": 1,
    "Module": "Lead",
    "sector": "Something"
}];


generateXLSX(res, data);
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