通过blob请求后端导出文件

后端controller

c 复制代码
 @PostMapping("/exportPlanProject2")
    public void exportActive(@RequestBody USER user, HttpServletResponse httpServletResponse) throws IOException {}

后端service

c 复制代码
    public void exportExcel2(HttpServletResponse response) throws IOException {
        OutputStream out = response.getOutputStream();
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding(Charsets.UTF_8.name());
            String fileName = "首页导出";
            fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            wb.write(out);
        } catch (Exception e) {
            log.error("导出Excel异常{}", e.getMessage());
            throw new CustomException("导出Excel失败,请联系网站管理员!");
        } finally {
            if (wb != null) {
                try {
                    wb.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

前端

c 复制代码
     request({
        method: 'post',
        url: '/investment/plan/remake/exportPlanProject2',
        data: this.queryParams,
        responseType: 'blob'
      }).then(res => {
        console.log('res---',res)
        const aLink = document.createElement('a')
        var fileName = '文件名称' + moment().format('YYYYMMDDHHmmss') + '.xlsx'
        fileName = fileName.replace(/\"/g, '')
        const url = window.URL.createObjectURL(res)
        console.log('url---',url)
        aLink.href = url
        aLink.download = fileName
        aLink.click()
        aLink.remove()
        URL.revokeObjectURL(url)
      })
相关推荐
Moshow郑锴2 天前
SpringBoot3+EasyExcel通过WriteHandler动态实现表头重命名
spring·excel·导出·阿里巴巴·easyexcel·speadsheet
如意机反光镜裸1 个月前
一键导出数据库表到Excel
数据库·excel·导出
孤蓬&听雨3 个月前
Java SpringBoot使用EasyExcel导入导出Excel文件
java·spring boot·excel·导出·导入
humors2213 个月前
java实现excel导入参考资料合集
java·前端·后端·wps·导出·导入·office
Mr。轩。5 个月前
cn.afterturn.easypoi.exception.excel.ExcelExportException: Excel导出错误 -> 修正过程。
java·excel·导出
大福是小强7 个月前
016_Save_the_picture_in_Matlab中保存图片
开发语言·matlab·导出·绘图·图片·出图
子不语7 个月前
c#实现数据导出为PDF的方式
pdf·c#·导出·文档·net
winfredzhang7 个月前
如何使用 Python创建一个视频文件管理器并实现视频截图功能
python·视频·导出·截图·搜索时长播放
winfredzhang9 个月前
使用python编程的视频文件列表应用程序
python·视频·导出·播放·搜素