In this tutorial, I'll show you how to export data to excel file using C# in ASP.NET MVC. The export to Excel functionality is implemented using the ClosedXML.Excel NuGet Package or download it directly from, as shown in this example. We will install ClosedXML.Excel library

The ClosedXML library makes it easier for developers to create Excel (.xlsx, .xlsm, etc) files. It provides a nice object orientation for manipulating files without having to handle the complexity of XML documents. It can be used by any .NET language such as C# and VB.NET.

public FileResult Export()
    if (TempData["Invoices"] != null)
        DataTable dt = new DataTable("Invoices");
        dt.Columns.AddRange(new DataColumn[7]
                    new DataColumn("RowNumber"),
                    new DataColumn("InvoiceNumber"),
                    new DataColumn("BuyerName"),
                    new DataColumn("BuyerAddress"),
                    new DataColumn("BuyerEmail"),
                    new DataColumn("Total"),
                    new DataColumn("InvoiceDate")
        foreach (var item in TempData["Invoices"] as List<InvoiceInfo> )
            dt.Rows.Add(item.RowNumber, item.InvoiceNumber, item.BuyerName, item.BuyerAddress, item.BuyerEmail, item.Total, item.InvoiceDate);
        using (XLWorkbook wb = new XLWorkbook())
            using (MemoryStream stream = new MemoryStream())
                return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Invoices.xlsx");
    throw new HttpException(404, "File not found !");

You can get data from TempData or get directly from your database, in this tutorial i'm using TempData

As you can see, ClosedXML allows you to create Excel 2007 version (.xlsx, .xlsm, etc) files without the Excel application. The typical example is creating Excel reports on a web server.