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

457
Views
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 answers
Answer question

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 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!