我如何使用 (Core 2.2) Web return 生成 XLSX API?

How do I return a generated XLSX using (Core 2.2) Web API?

现在,作为序言:我已经查看了一些明显的答案,无论出于何种原因,其中 none 似乎对我有用 - 至少对于 EPPlus 4.5.3.2 / Core 2.2 网络 API

问题:

我的任务是将 List<ExpandObject> 的结果集转换为 XLSX(使用 EPPlus),通过 Web API.

使用

解决方案(?)

这是我目前的解决方案 - 问题是它只是 returns 一个结构化的 json 而忽略了 XLSX 附件:

[HttpGet]
public HttpResponseMessage GetInventoryExcel() {
   List<ExpandObject> results = MagicallyGenerateList();
   return ToExcelResponse("example", results.ToArray());
}

public HttpResponseMessage ToExcelResponse(string filename, dynamic[] results) {
    var response = new HttpResponseMessage(HttpStatusCode.OK);

    using (MemoryStream memoryStream = new MemoryStream()) {
        using (var package = new ExcelPackage(memoryStream)) {
            var worksheet = package.Workbook.Worksheets.Add(filename);
            int x = 1, y = 0;

            foreach (var result in results) {
                x = 1;
                y++;

                foreach (var key in ((IDictionary<string, object>)result).Keys) {
                    if (((IDictionary<string, object>)result).TryGetValue(key, out var value)) {
                        worksheet.Cells[x++, y].Value = value;
                    } else {
                        x++;
                    }
                }
            }

            package.Save();
        }

        response.Content = new StreamContent(memoryStream);
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
            FileName = filename + ".xlsx"
        };
    }

    return response;
}

示例响应

{
    "Version": {
        "Major": 1,
        "Minor": 1,
        "Build": -1,
        "Revision": -1,
        "MajorRevision": -1,
        "MinorRevision": -1
    },
    "Content": {
        "Headers": [
            {
                "Key": "Content-Type",
                "Value": [
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                ]
            },
            {
                "Key": "Content-Disposition",
                "Value": [
                    "attachment; filename=example.xlsx"
                ]
            }
        ]
    },
    "StatusCode": 200,
    "ReasonPhrase": "OK",
    "Headers": [],
    "RequestMessage": null,
    "IsSuccessStatusCode": true
}

我尝试使用 [Produces("application/ms-excel")] 指令,但这只会导致 406 响应代码。任何建议将不胜感激

经过一些小改动,它对我有用:

    [HttpGet]
    public IActionResult GetInventoryExcel()
    {
        List<ExpandoObject> results = MagicallyGenerateList();
        return ToExcelResponse("example", results.ToArray());
    }

    public IActionResult ToExcelResponse(string filename, dynamic[] results)
    {
        MemoryStream memoryStream = new MemoryStream();
        using (var package = new ExcelPackage(memoryStream))
        {
            var worksheet = package.Workbook.Worksheets.Add(filename);
            int x = 1, y = 0;

            foreach (var result in results)
            {
                x = 1;
                y++;

                foreach (var key in ((IDictionary<string, object>)result).Keys)
                {
                    if (((IDictionary<string, object>)result).TryGetValue(key, out var value))
                    {
                        worksheet.Cells[x++, y].Value = value;
                    }
                    else
                    {
                        x++;
                    }
                }
            }

            package.Save();

            memoryStream.Position = 0;
            return File(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", filename + ".xlsx");
        }
    }

我用了这篇文章:https://www.c-sharpcorner.com/article/using-epplus-to-import-and-export-data-in-asp-net-core/