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

447
Views
¿Cómo llenar dinámicamente un archivo de Excel usando exceljs en express?

Ahora mismo tengo la siguiente función:

 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(); }) };

Esto editará mi documento base de Excel y producirá esto:

Excel editado a través de exceljs

El problema aquí es que quiero completar esta plantilla usando un objeto JSON como el siguiente:

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

Como puede ver, contiene datos que no quiero mostrar en Excel. Quiero implementar una forma dinámica de asignar la información sin escribir el mismo código como:

 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';

Y así sucesivamente, pero no sé cómo implementar una forma óptima de resolver esto...

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

0

necesita hacer un bucle de los datos y escribirlos en las celdas deseadas.

prueba esto

 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!