2017-01-21 155 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);代替。