gpt4 book ai didi

node.js - writeFile 在 exceljs 中不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 00:38:30 25 4
gpt4 key购买 nike

我正在使用 exceljs并尝试在单元格中写入值,但它不起作用。然而 workbook.xlsx.readFile(filename) 正在工作,但 workbook.xlsx.writeFile(filename) 不会。

这是我的代码:-

var Excel = require('exceljs');
var fs = require('fs')
module.exports.summary = function(req, res, next) {
try {
var filename = process.cwd() + '/template/report/summary.xlsx';
var workbook = new Excel.Workbook();
workbook.xlsx.writeFile(filename)
.then(function() {
var worksheet = workbook.getWorksheet(1);
console.log('worksheet',worksheet);
var row = worksheet.getRow(5);
row.getCell('C').value = new Date();
row.commit();
worksheet.commit();

workbook.commit().then(function() {
console.log('xls file is written.');
});
res.json({ msg: done })
});

} catch (e) {
next(e);
}
}

最佳答案

试试这个代码,希望这对你有帮助

const excel = require('exceljs');

//Creating New Workbook
var workbook = new excel.Workbook();

//Creating Sheet for that particular WorkBook
var sheetName = 'Sheet1';
var sheet = workbook.addWorksheet(sheetName);

//Header must be in below format
sheet.columns = [{key:"name", header:"name"}, {key: "age", header: "age"}];

//Data must be look like below, key of data must be match to header.
var data = [{name:"Kalai", age: 24}, {name:"Vignesh", age:24}];

//adding each in sheet
for(i in data){
sheet.addRow(data[i]);
}

//Finally creating XLSX file
var fileName = "Sample.xlsx";
workbook.xlsx.writeFile(fileName).then(() => {
callback(null);
});

关于node.js - writeFile 在 exceljs 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43279928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com