【问题标题】:How to dynamically populate an Excel file using exceljs in express?如何在 express 中使用 exceljs 动态填充 Excel 文件?
【发布时间】:2022-01-30 00:10:44
【问题描述】:

现在我有以下功能:

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

这将编辑我的基本 Excel 文档并生成:

这里的问题是我想使用 JSON 对象填充这个模板,如下所示:

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

如您所见,它包含我不想呈现到 Excel 中的数据。 我想实现一种动态的方式来分配信息,而无需编写相同的代码,例如:

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

等等,但我不知道如何实现解决这个问题的最佳方法......

【问题讨论】:

    标签: javascript node.js excel express exceljs


    【解决方案1】:

    您需要循环数据并将其写入所需的单元格。

    试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-06
      • 2022-06-12
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多