ClosedXML.Extensions.WebApi
ClosedXML.Extensions.WebApi copied to clipboard
ClosedXML Extensions WebAPI does not work with ASP .NET Core Web API
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:
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;
}
}