如何在 fastify 中使用 exceljs?
how can i use exceljs in fastify?
我像这样使用 exceljs 但它 returns 出错了。
TypeError: pipe.write is not a function
和
"message": "Cannot read property 'summary' of undefined"
let workbook = new Excel.Workbook()
let worksheet = workbook.addWorksheet('calogs')
worksheet.columns = [
{header: 'id', key: 'id', width: 5 },
...
]
// Add Array Rows
worksheet.addRows(data)
console.log('XXXXXXXXXXXXXXXXXXXXX',worksheet)
// res is a Stream object???
reply.header(
'Content-Type',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
reply.header(
'Content-Disposition',
`attachment; filename=` +`calogs.xlsx`
)
return workbook.xlsx.write(reply).then(function() {
reply.status(200).end()
})
我该如何解决?
reply
对象不是流,您可以尝试 reply.raw
是原始的 HTTP Server Response。
无论如何,回复可以为您解决这个问题,这里是 xlsx
模块的示例:
const XLSX = require('xlsx')
const fastify = require('fastify')()
fastify.get('/', function (request, reply) {
const wb = XLSX.utils.book_new()
const wsName = 'SheetJS'
const wsData = [
['S', 'h', 'e', 'e', 't', 'J', 'S'],
[1, 2, 3, 4, 5]
]
const ws = XLSX.utils.aoa_to_sheet(wsData)
XLSX.utils.book_append_sheet(wb, ws, wsName)
const wopts = { bookType: 'xlsx', bookSST: false, type: 'buffer' }
const wBuffer = XLSX.write(wb, wopts)
reply.header('Content-Type',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
reply.header('Content-Disposition',
'attachment; filename=' + 'calogs.xlsx'
)
reply.send(wBuffer)
})
fastify.listen(8080)
谢谢:
现在我用 reply.raw 这样做,它有效。
let workbook = new Excel.Workbook()
let worksheet = workbook.addWorksheet('calogs')
worksheet.columns = [
???datasformat
]
// Add Array Rows
worksheet.addRows(data)
let date = new Date()
date = moment(date).format('YYYYMMDD')
let filename = `excelfile${date}.xlsx`
// res is a Stream object???
reply.raw.writeHead(200, {
'Content-Type':
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
})
reply.raw.writeHead(200, {
'Content-Disposition': 'attachment; filename=' + filename
})
await workbook.xlsx.write(reply.raw)
reply.sent = true
20210721
虽然上面的方法可以提供帮助。
但是当你使用 fastify-helmet 时会 return 报错。
所以请这样做:
const workbook = new this.$Excel.Workbook()
const worksheet = workbook.addWorksheet('caLogs')
worksheet.columns = [
{ header: 'serialNo', key: 'id', width: 5 },
...
]
worksheet.addRows(data)
const date = this.$dayjs(new Date()).format('YYYYMMDD')
const fileName = `logs${date}.xlsx`
reply.header(
'Content-Type',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
reply.header('Content-Disposition', 'attachment; filename=' + fileName)
const buffer = await workbook.xlsx.writeBuffer()
reply.send(buffer)
}
我像这样使用 exceljs 但它 returns 出错了。
TypeError: pipe.write is not a function
和
"message": "Cannot read property 'summary' of undefined"
let workbook = new Excel.Workbook()
let worksheet = workbook.addWorksheet('calogs')
worksheet.columns = [
{header: 'id', key: 'id', width: 5 },
...
]
// Add Array Rows
worksheet.addRows(data)
console.log('XXXXXXXXXXXXXXXXXXXXX',worksheet)
// res is a Stream object???
reply.header(
'Content-Type',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
reply.header(
'Content-Disposition',
`attachment; filename=` +`calogs.xlsx`
)
return workbook.xlsx.write(reply).then(function() {
reply.status(200).end()
})
我该如何解决?
reply
对象不是流,您可以尝试 reply.raw
是原始的 HTTP Server Response。
无论如何,回复可以为您解决这个问题,这里是 xlsx
模块的示例:
const XLSX = require('xlsx')
const fastify = require('fastify')()
fastify.get('/', function (request, reply) {
const wb = XLSX.utils.book_new()
const wsName = 'SheetJS'
const wsData = [
['S', 'h', 'e', 'e', 't', 'J', 'S'],
[1, 2, 3, 4, 5]
]
const ws = XLSX.utils.aoa_to_sheet(wsData)
XLSX.utils.book_append_sheet(wb, ws, wsName)
const wopts = { bookType: 'xlsx', bookSST: false, type: 'buffer' }
const wBuffer = XLSX.write(wb, wopts)
reply.header('Content-Type',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
reply.header('Content-Disposition',
'attachment; filename=' + 'calogs.xlsx'
)
reply.send(wBuffer)
})
fastify.listen(8080)
谢谢:
现在我用 reply.raw 这样做,它有效。
let workbook = new Excel.Workbook()
let worksheet = workbook.addWorksheet('calogs')
worksheet.columns = [
???datasformat
]
// Add Array Rows
worksheet.addRows(data)
let date = new Date()
date = moment(date).format('YYYYMMDD')
let filename = `excelfile${date}.xlsx`
// res is a Stream object???
reply.raw.writeHead(200, {
'Content-Type':
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
})
reply.raw.writeHead(200, {
'Content-Disposition': 'attachment; filename=' + filename
})
await workbook.xlsx.write(reply.raw)
reply.sent = true
20210721 虽然上面的方法可以提供帮助。 但是当你使用 fastify-helmet 时会 return 报错。 所以请这样做:
const workbook = new this.$Excel.Workbook()
const worksheet = workbook.addWorksheet('caLogs')
worksheet.columns = [
{ header: 'serialNo', key: 'id', width: 5 },
...
]
worksheet.addRows(data)
const date = this.$dayjs(new Date()).format('YYYYMMDD')
const fileName = `logs${date}.xlsx`
reply.header(
'Content-Type',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
reply.header('Content-Disposition', 'attachment; filename=' + fileName)
const buffer = await workbook.xlsx.writeBuffer()
reply.send(buffer)
}