ClosedXML.Extensions.WebApi icon indicating copy to clipboard operation
ClosedXML.Extensions.WebApi copied to clipboard

ClosedXML Extensions WebAPI does not work with ASP .NET Core Web API

Open augustoproiete opened this issue 4 years ago • 1 comments

Following up from @Pankraty's comment on ClosedXML/#1283... Looking at the different ClosedXML extensions, it seems ClosedXML.Extensions.AspNet would be the most appropriate extension for ASP .NET Core Web API projects, so the below is just for awareness.


Instead of the file being downloaded, we receive a text response:

image

Sample project that reproduces the above:


NuGet packages added to the project:

<PackageReference Include="ClosedXML" Version="0.94.2" />
<PackageReference Include="ClosedXML.Extensions.WebApi" Version="0.2.1" />

Example code from the README:

    [ApiController]
    public class ExcelController : ControllerBase
    {
        [HttpGet]
        [Route("api/file/{id}")]
        public async Task<HttpResponseMessage> DownloadFile(int id)
        {
            var wb = await BuildExcelFile(id);
            return wb.Deliver("excelfile.xlsx");
        }

        private async Task<XLWorkbook> BuildExcelFile(int id)
        {
            var t = Task.Run(() =>
            {
                var wb = new XLWorkbook();
                var ws = wb.AddWorksheet("Sheet1");
                ws.FirstCell().SetValue(id);

                return wb;
            });

            return await t;
        }
    }

augustoproiete avatar Aug 30 '19 15:08 augustoproiete