如何在 express 中使用 exceljs 动态填充 Excel 文件?
How to dynamically populate an Excel file using exceljs in express?
现在我有以下功能:
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';
等等,但我不知道如何实现解决这个问题的最佳方法...
您需要循环数据并将其写入所需的单元格。
试试这个
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);
现在我有以下功能:
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';
等等,但我不知道如何实现解决这个问题的最佳方法...
您需要循环数据并将其写入所需的单元格。
试试这个
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);