如何使用npm filesaver导出角度和web api中的excel [英] How to export to excel in angular and web api using npm filesaver
本文介绍了如何使用npm filesaver导出角度和web api中的excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望使用npm filesaver导出到angular和web api中的excel
在控制台中我收到错误消息:
请求正文不是blob或数组缓冲区
这是我的代码
Web Api:
I want export to excel in angular and web api using npm filesaver
In Console I am getting the error message :
The request body isn't either a blob or an array buffer
Here is my code
Web Api :
public HttpResponseMessage GetExcelData()
{
try
{
JobAssignment j = new JobAssignment();
string filePath = j.GetExcelData();
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = Path.GetFileName(filePath) };
// result.Content.Headers.ContentLength = stream.Length;
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
return result;
}
catch (Exception ex)
{
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.InternalServerError);
return result;
}
}
组件:
Component :
GetExcelData() {
this.loaderService.GetExcelData()
.subscribe(
blob => {
importedSaveAs(blob, "file1");
},
error => {
this.errorMessage = <any>error
});
}
服务:
service :
GetExcelData(): Observable<Blob> {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers, responseType: ResponseContentType.Blob });
let bodyString = {};
return this.http.get(this.rootUrl + 'GetExcelData',{responseType: ResponseContentType.Blob})
.map(res => res.blob())
.catch(this.handleError);
}
我尝试过:
我已更改
What I have tried:
I have changed
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/vnd.ms-excel");
但它不起作用。
请告诉我哪里错了。
But it is not working.
Please tell me where I am wrong.
推荐答案
好的,我得到了解决方案,非常简单,我们就可以做到。
Web Api
Ok I got the solution, very simply we can do it.
Web Api
public void GetExcelData()
{
string sFileName = System.IO.Path.GetRandomFileName();
string sGenName = "Friendly.xls";
System.IO.FileStream fs = null;
fs = System.IO.File.Open(HttpContext.Current.Server.MapPath("~/TextFiles/StudentWiseRegistrationReport__14082017_134859.xls"), System.IO.FileMode.Open);
byte[] btFile = new byte[fs.Length];
fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
fs.Close();
HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename=" + sGenName);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.BinaryWrite(btFile);
HttpContext.Current.Response.End();
}
在组件中:
And In Component:
window.location.href='http://localhost:1036/api/Employee/GetExcelData';
Excel表或文本文件可以这种方式下载。
Excel sheet or text file can be downloaded in this way.
这篇关于如何使用npm filesaver导出角度和web api中的excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文