【发布时间】:2018-08-23 01:02:00
【问题描述】:
我从 Angular 向 C# Api 发布了一个帖子,成功通过并使用 (ClosedXML) 安装了 Excel。
我可以随意格式化并保存我完成的 Excel 文件。但我需要将此 Excel 作为二进制或类似于页面中 Angular 6 按钮中的下载事件返回给 Angular。
我知道错误出现在最后几行代码中,因为我有,如果我愿意,我可以在本地保存我的 excel 文件,
C# API 方法结束:
[HttpPost]
[Route("api/qqq/generateExcel")]
public Task<HttpResponseMessage> Post(List<MyObject> list)
{
...
var localWb = new XLWorkbook();
localWb = arquivo.GerarExcel(listaRegras);
//----------------------
var filePath = "C:\\Users\\folder";
var fileName = "Export.xlsx";
using (MemoryStream ms = new MemoryStream())
{
localWb.SaveAs(ms);
using (FileStream file = new FileStream(filePath + "\\Export.xlsx", FileMode.Open, FileAccess.Read))
{
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
httpResponseMessage.Content = new ByteArrayContent(ms.GetBuffer());
httpResponseMessage.Content.Headers.Add("x-filename", fileName);
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
httpResponseMessage.Content.Headers.ContentDisposition.FileName = fileName;
httpResponseMessage.Content.Headers.ContentLength = file.Length;
httpResponseMessage.StatusCode = HttpStatusCode.OK;
// return httpResponseMessage;
return CreateResponse(HttpStatusCode.OK, httpResponseMessage);
}
}
我的角度服务:
gerarExcel(listaXYZ) {
return this.http.post(this.apiUrl, listaXYZ);
}
** 组件**
exportToExcel(event): void {
let lista= this.regras;
let configs = '{ "config1": "valorConfig1", "config2": "valorConfig2" }';
console.log(listaXYZ);
//console.log(lista.length);
if (lista != null) {
this.myservice
.gerarExcel(listaXYZ)
.subscribe(data => {
this.downloadFile(data)
console.log(<any>data);
},
(error: HttpErrorResponse) => {
...
},
() => {
...
}
);
}
else {
...
}
downloadFile(data: IDownloadArquivo) {
var url = window.URL.createObjectURL(data.blob);
let a = document.createElement("a");
document.body.appendChild(a);
a.style.display = "none";
a.href = url;
a.target = "_blank";
a.download = data.fileName;
a.click();
a.remove();
}
我该怎么做,什么都没有???
【问题讨论】:
标签: export-to-excel angular6 closedxml