【问题标题】:Angular convert excel corrupted角转换excel损坏
【发布时间】:2020-10-29 13:08:15
【问题描述】:

我的项目使用 angular,现在调用 api 来获取 excel 文件的 byte[]。但是当我通过 blob 将 byte[] 转换为文件时,该文件已损坏。有人可以帮我吗
我的 Angular 是第 9 版

service.ts:

downloadFile(req?: any): any {
    const options = createRequestOption(req);
    return this.http.get(`${this.resourceUrl}/print`, {
       params: options,
       responseType:'blob' })
   .toPromise();
}

component.ts:

  import {saveAs} from 'file-saver';

  export():void{
    this.exportService.downloadFile({}) .then((blob:any)=> {
        saveAs(blob, 'test.xlsx');
        });
     }
  }

API 响应

我的excel文件:

【问题讨论】:

    标签: arrays angular typescript blob export-to-excel


    【解决方案1】:

    请更改您的回复类型如下:

    downloadFile(req?: any): any {
        const options = createRequestOption(req);
        return this.http.get(`${this.resourceUrl}/print`, {
           params: options,
           responseType: ResponseContentType.Blob })
       .toPromise();
    }
    

    通过添加 Blob

    将您的代码更改为以下代码
    export():void{
        this.exportService.downloadFile({}) .then((data:any)=> {
    
    let blob = new Blob([data], {type: 'text/csv'});
            saveAs(blob, 'test.xlsx');
            });
         }
    

    【讨论】:

    • 现在我已经改变了答案,请告诉我它是否有效。
    • 我收到错误:“找不到名称 ResponseContentType”。啊,我的角度是版本 9
    • 将 responseType: ResponseContentType.Blob 更改为 responseType: 'blob' as 'blob'
    • 没有调试我们无法给出答案。请创建一个堆栈闪电战。
    【解决方案2】:

    不要一个人,tks。我不确定什么是最好的答案,但这项工作

     testByte(filename = ''): Observable<HttpResponse<string>>{
      return this.http.get(`${this.resourceUrl}/testByte`, {
        responseType: 'text',
        observe: 'response'
      }).pipe(map((res: HttpResponse<string>) => this.byteToFile(res,filename)));
    }
    
    
    private byteToFile(res: HttpResponse<string>,filename:string): HttpResponse<string> {
        if(res.body){
         const fileName = filename;
         const a = document.createElement('a');
         document.body.appendChild(a);
         const sliceSize = 512;
    
         const byteCharacters = res.body;
         const byteArrays = [];
    
         for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
           const slice = byteCharacters.slice(offset, offset + sliceSize);
    
           const byteNumbers = new Array(slice.length);
           for (let i = 0; i < slice.length; i++) {
             byteNumbers[i] = slice.charCodeAt(i);
           }
    
          const byteArray = new Uint8Array(byteNumbers);
    
          byteArrays.push(byteArray);
        }
    
        const blob = new Blob(byteArrays, {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
        const fileURL = window.URL.createObjectURL(blob);
        a.href = fileURL;
        a.download = fileName;
        a.click();
      }
      return res.clone();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      相关资源
      最近更新 更多