如何从 Angular 11 中的 rest API 下载和保存 excel

问题描述

我在我的 angular 应用程序中使用以下代码。通过 swagger 单独调用时,Spring Boot 后端 api 正在生成和保存 excel。但是,当使用以下代码从前端 angular 发送时,会下载 excel,但在尝试打开时出现错误提示文件格式或扩展名无效,因此无法打开 excel

component.ts

this.someService.getSomeList(someListObject)
            .subscribe(response=> {
                this.downloadFile(response);
            });
    downloadFile(response: Blob) {
    const blob = new Blob([response],{ type: 'application/octet-stream' });
    FileSaver.saveAs(blob,'FileName' + '.xlsx');

    
      }

service.ts

 getSomeList(someListObject: ListClass) {
  console.log("file downloaded"); 
 return this.http.post(this.downloadListURL,someListObject,{ responseType: 'blob' });

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)