Skip to content

Latest commit

 

History

History
46 lines (37 loc) · 1.01 KB

README.md

File metadata and controls

46 lines (37 loc) · 1.01 KB

ClosedXML.Extensions.WebApi

WebApi Extensions for ClosedXML

Install via NuGet

To install ClosedXML.Extensions.WebApi, run the following command in the Package Manager Console

PM> Install-Package ClosedXML.Extensions.WebApi

or

dotnet add package ClosedXML.Extensions.WebApi

Usage

In your WebApi controller define an action that will generate and download your file:

public class ExcelController : ApiController
{
    [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)
    {
        //Creating the workbook
        var t = Task.Run(() =>
        {
            var wb = new XLWorkbook();
            var ws = wb.AddWorksheet("Sheet1");
            ws.FirstCell().SetValue(id);

            return wb;
        });

        return await t;
    }
}