angular4 下載文件 Excel zip包


1.下載Excel 文件

import { ResponseContentType } from '@angular/http'; 
exportInternalOrder(body) {
    let user_token: string = this.sessionService.getToken();
    let headers = new Headers();
    headers.append('Authorization', 'Bearer ' + user_token);
    return this.http.post(this.config.exportInternalOrder, body,{
        headers: headers,
        responseType: ResponseContentType.Blob
    }).map(res => new Blob([res._body],{ type: 'application/vnd.ms-excel' }));
}

 

2.下載ZIP

var a = document.createElement('a');
var blob = new Blob([responseData], {'type':"application/octet-stream"});
a.href = URL.createObjectURL(blob);
a.download = "filename.zip";
a.click();

 瀏覽器有時候會攔截窗口 所以下載excel zip 文件的時候創建a標簽下載 保存到本地即可。如上zip下載示例。

 下載示例:

  /**
   * 請求成功之后保存成excel文件(基本導出方法,請求成功之后調用)
   * @param data
   * @param name
   */
 public  saveExcel(data: Response,name:string){
    var a = document.createElement('a');
    var blob = new Blob([data], {'type':"application/vnd.ms-excel"});
    a.href = URL.createObjectURL(blob);
    a.download = name+".xls";
    a.click();
  }  

public  down(startDate:String,endDate:String):Promise<any>{
     const url= `rest/report/export?startDate=`+startDate+'&endDate='+endDate;
      return this.http.get(url,{responseType: ResponseContentType.Blob}).toPromise().then(res=>{
        //return res.json() as any;
         this.saveExcel(res.json(),"表一");
      })
  }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM