【问题标题】:Return xlsx data in Task<IHttpActionResult> with EPPlus使用 EPPlus 返回 Task<IHttpActionResult> 中的 xlsx 数据
【发布时间】:2018-01-07 15:43:53
【问题描述】:

我有一个基本的 web api,它试图返回一个作为下载操作而不是数据流触发的 excel 文档。关于创建流和设置附件属性,我在网上遵循了许多示例,但我的 Postman 请求始终显示为已编码。下面是API方法。

[Route("getexcel")]
[HttpPost]
public async Task<IHttpActionResult> GetExcel()
{
   IHttpActionResult result = null;
   FileInfo newFile = new FileInfo(@"C:\Test.xlsx");
   ExcelPackage pck = new ExcelPackage(newFile);

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

   result = ResponseMessage(response);
   return result;
}

从上面的代码中,如果我使用 MediaTypeHeaderValue("application/pdf") 那么 Postman 请求会显示下载请求,但对于 xlsx 格式或 application/octet-stream 则不会。相反,它显示了一个数据流。 Content-Type 是 application/json 因为最终版本的 api 将在 body 中获取 json 数据。

【问题讨论】:

    标签: excel api postman xlsx epplus


    【解决方案1】:

    我可能会迟到,但是... 发送按钮的下拉菜单中有“发送和下载”

    【讨论】:

    • 这是正确的解决方案,我在创建问题后几天才意识到这一点,忘记更新了,所以感谢您的回复。
    猜你喜欢
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    相关资源
    最近更新 更多