后端写法

func CapitalFlowExport(c *gin.Context) {
	file := xlsx.NewFile()
	// 这中间自己写业务逻辑生成excel表
	// 我用的是	"github.com/tealeg/xlsx" 这个框架  v1.0.0 版本
	c.Header("responseType", "blob")
	c.Header("Content-Disposition", "filename=excel名称.xlsx")
	c.Header("Content-Type", "application/x-excel")
	file.Write(c.Writer)
}

前端写法

export(query).then((data) => {
   const url = window.URL.createObjectURL(new Blob([res.data]))
   const link = document.createElement('a')
   link.href = url
   link.setAttribute('download', 'excel名称.xlsx')
   document.body.appendChild(link)
   link.click()
 }).catch(e => {
   console.log('err', e)
 })

请求的js
这里的fetch是自己封装的axios请求

export function export(data) {
  return fetch({
    url: '/export',
    method: 'post',
    responseType: 'blob',
    data:data
  })
}