gpt4 book ai didi

javascript - 使用closedxml从web api下载excel文件

转载 作者:行者123 更新时间:2023-11-30 15:05:18 25 4
gpt4 key购买 nike

我无法通过 Web API 下载由 closedxml 创建的 excel 文件。如果我将文件保存在服务器上,它看起来不错,但一旦我将其放入流中并将其返回到 Web API,那么浏览器中只会收到损坏的文件。

正如我使用 httpResponseMessage 的几篇文章所建议的那样,但在浏览器中, header 中的文件名也永远不会到达。

我们正在使用:

"Microsoft.AspNet.WebApi"version="5.2.3"targetFramework="net461

"ClosedXML"version="0.88.0"targetFramework="net461"

WebAPI 代码:

 var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Parcel List");


MemoryStream fs = new MemoryStream();
wb.SaveAs(fs);
fs.Position = 0;


HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(fs.GetBuffer());
result.Content.Headers.ContentLength = fs.Length;
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "List" + "_" + DateTime.Now.ToShortDateString() + ".xlsx"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

return result;

这里是javascript代码:

  context.$http.post(config.get_API_URL() + 'api_call',  excel_list,
{responseType: 'application/octet-stream'})
.then(
success_function,
error_function)
}

成功函数:

function(response) {

var headers = response.headers;
var blob = new Blob([response.body],
{type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'},
);

window.open(window.URL.createObjectURL(blob));

}

最佳答案

我现在可以使用此代码成功下载工作簿:

using ClosedXML.Excel;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;

namespace ClosedXML.Extensions.WebApi.Controllers
{
public class ValuesController : ApiController
{
public IHttpActionResult Get(int id)
{
return new TestFileActionResult(id);
}
}

public class TestFileActionResult : IHttpActionResult
{
public TestFileActionResult(int fileId)
{
this.FileId = fileId;
}

public int FileId { get; private set; }

public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
HttpResponseMessage response = null;

var ms = new MemoryStream();
using (var wb = new XLWorkbook())
{
var ws = wb.AddWorksheet("Sheet1");
ws.FirstCell().Value = this.FileId;

wb.SaveAs(ms);

ms.Seek(0, SeekOrigin.Begin);

response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(ms);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "test.xlsx";
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

response.Content.Headers.ContentLength = ms.Length;
ms.Seek(0, SeekOrigin.Begin);
}

return Task.FromResult(response);
}
}
}

关于javascript - 使用closedxml从web api下载excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45855468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com