这是我使用 exceljs 编写和下载 excel 文件的代码。
const excel = require('exceljs')
const tempfile = require('tempfile')
var workbook = new excel.Workbook()
var sheet1 = workbook.addWorksheet('sample')
sheet1.columns = req.keys // Some data
var tempFilePath = tempfile('.csv')
workbook.csv.writeFile(tempFilePath).then(function() {
res.download(tempFilePath, 'sample.csv', function(err) {
if (err) {
res.status(500).json({
"success": false,
"error": err
})
return
}
})
})
当我将 csv 替换为 xlsx 时,它会写入但文件已损坏。
const excel = require('exceljs')
const tempfile = require('tempfile')
var workbook = new excel.Workbook()
var sheet1 = workbook.addWorksheet('sample')
sheet1.columns = req.keys // Some data
var tempFilePath = tempfile('.xlsx')
workbook.xlsx.writeFile(tempFilePath).then(function() {
res.download(tempFilePath, 'sample.xlsx', function(err) {
if (err) {
res.status(500).json({
"success": false,
"error": err
})
return
}
})
})
已在此处附上它的快照。