hooks icon indicating copy to clipboard operation
hooks copied to clipboard

使用 buffer 导出的 excel 文件无法打开

Open Ttou opened this issue 3 years ago • 1 comments

示例项目

核心代码:

import { Api, Post, useContext } from '@midwayjs/hooks'
import { Context } from '@midwayjs/koa'
import ExcelJS from 'exceljs'
import path from 'path'

export const demoExport = Api(Post(), async () => {
  const ctx = useContext<Context>()

  const wb = new ExcelJS.Workbook()
  const ws = wb.addWorksheet('Demo')

  ws.columns = [{ header: '编号', key: 'id' }]
  ws.addRows([{ id: 1 }, { id: 2 }])

  // 写入的文件可以打开
  wb.xlsx.writeFile(path.resolve('./demo.xlsx'))

  // 返回的 buffer 无法打开
  const buffer = await wb.xlsx.writeBuffer()

  ctx.set('content-type', 'application/vnd.openxmlformats;charset=utf-8')
  ctx.set('content-disposition', `attachment; filename=demo.xlsx`)

  ctx.body = buffer
})

Ttou avatar Jul 01 '22 11:07 Ttou

就没有人解决或者给个临时的方案吗

Ttou avatar Jul 27 '22 04:07 Ttou