0

I use exceljs like this but it returns a error.

TypeError: pipe.write is not a function

and

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

how can i fix it?

radiorz
  • 1,459
  • 4
  • 18
  • 36
  • https://stackoverflow.com/questions/43279928/writefile-not-woking-in-exceljs —> This link might help you out and can I see your imports of that file please? – Maurice Pheyton Nov 27 '20 at 02:22

2 Answers2

1

The reply object is not a stream, you could try reply.raw that is the raw HTTP Server Response.

Anyway, the reply could manage this for you, here an example with xlsx module:

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)
Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73
0

Thank you:

and now i do this way with reply.raw ,it works.

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 althrough the method above can help. but when you use fastify-helmet will return a error. so pleace do like this:

    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)
  }
radiorz
  • 1,459
  • 4
  • 18
  • 36