2017-01-21 154 views
-1

我已經創建了示例項目以使用JExcelApi lib以excel格式導出數據。項目有Angular 2前端和spring mvc(rest api)後端。如果我將後端部署到tomcat並通過瀏覽器發出請求,那麼它會正確地導出excel文件,但是當我使用angular 2(typescript)進行相同的http調用時,它會在我的api文件中寫入我的api的url,並且它顯示爲損壞的excel文件。Angular 2 excel導出不起作用

項目位於以下github上的位置

NG2 Excel Export demo

有人能告訴我什麼是錯我的服務或app.component.ts文件?

這裏是app.component.ts代碼庫,我認爲這是導致該問題的

downloadExcel(){ 
console.log("Downloading excel from server....") 
this.fileService.downloadFile() 
    .subscribe(data => window.open(window.URL.createObjectURL(data)), 
     error => console.log("Error downloading the file."), 
     () => console.log('Completed file download.')); 

}

,這裏是服務類返回數據

downloadFile(){ 
    let url = "http://localhost:9999/api/download"; 
    var headers = new Headers(); 
    headers.append('responseType', 'arraybuffer'); 
    return this._http.get(url) 
     .map(res => new Blob([res],{ type: 'application/vnd.ms-excel' })) 
     .catch((error : any) => Observable.throw(error)); 
} 
+0

沒有人會通過你的整個存儲庫查看錯誤。您需要具體並明確問題出在哪裏,然後**我們可以幫助 –

+0

對不起,我更新了代碼片段的原始評論,我認爲這是問題所在。我也嘗試了responsType Blob,但仍然沒有運氣 – user509755

回答

0

我已經解決了FileSaver.js的問題,以防萬一某人有這個問題。看看上面的github項目

0

改用window.location.assign(link);代替。