Excel Export/Download In ASP.NET Core

In this tutorial, I'll teach you a quick and easy way to export/download `IEnumerable<T>` data into an Excel file in Asp.Net Core Backend. There are lots of tutorials available, but this tutorial will be super simple to implement and won't take more than 10 minutes of your time.

Before we proceed, install the following Nuget Package.

Fingers10.ExcelExport v1.0.0

Package Manager

PM> Install-Package Fingers10.ExcelExport

.Net CLI

dotnet add package Fingers10.ExcelExport

Generally, we always want to export lists of data into Excel in most cases. After analyzing this with many solutions available on the internet, I have come up with an easy approach to implement this functionality. I have done all the hard work in my Nuget Package - Fingers10.ExcelExport. All you need to do is to follow the below steps.

  1. Get your IEnumerable data from the database or service.
  2. I have created an Action Result named ExcelResult<T> in my package. Now in your action method/page handler make a call to this ExcelResult and pass your data, sheet name, and file name as parameters.
  3. That's it -- this will take care of processing your data and return it back to the browser as an Excel file.
  4. Your data can have any level of nesting.
  5. You can customize the Excel column header names using Data Annotations as explained below.
  6. The Excel will have columns displayed in the order of properties that you have in your data model.

Let's say we have a model like the below one. Note that we can have Nested Properties up to any level and they are displayed in Excel in the order we add the property in the class.

Root Model

public class DemoExcel  
{  
    public int Id { get; set; }  
    public string Name { get; set; }  
    public string Position { get; set; }  
    [Display(Name = "Office")]  
    public string Offices { get; set; }  
    public DemoNestedLevelOne DemoNestedLevelOne { get; set; }  
}  

Nested level one model

public class DemoNestedLevelOne  
{  
    public short? Experience { get; set; }  
    [DisplayName("Extn")]  
    public int? Extension { get; set; }  
    public DemoNestedLevelTwo DemoNestedLevelTwos { get; set; }  
}  

Nested level two model

public class DemoNestedLevelTwo  
{  
    [DisplayName("Start Date")]  
    public DateTime? StartDates { get; set; }  
    public long? Salary { get; set; }  
}  

Column Names

As mentioned earlier, Column names in Excel Export can be configured using the below attributes.

  • * `[Display(Name = "")]`
  • * `[DisplayName(“”)]`

If the above attributes are not used then the column name will default to property name.

Action Method

Here comes the cool part. Now in your action method, get your IEnumerable<T> data from the service and then return it as an ExcelResult<T> by specifying the Root Model sheet name and Excel file name.

public async Task<IActionResult> GetExcel()
{
    // Get you IEnumerable<T> data
    var results = await _demoService.GetDataAsync();
    return new ExcelResult<DemoExcel>(results, "Demo Sheet Name", "Fingers10");
}

Page Handler

The same goes for page handlers. Now in your page handler, get your IEnumerable<T> data from the service and then return it as an ExcelResult<T> by specifying the Root Model sheet name and Excel file name.

public async Task<IActionResult> OnGetExcelAsync()
{
    // Get you IEnumerable<T> data
    var results = await _demoService.GetDataAsync();
    return new ExcelResult<DemoExcel>(results, "Demo Sheet Name", "Fingers10");
}

Output

Now the Excel file will be downloaded as shown below,

If you have a look at the generated Excel, you can see that the columns are ordered the same as our data model. Id, Name, Position, and Office from DemoExcel class, and the last property in our DemoExcel class is a ComplexType which is DemoNestedLevelOne which in turn has Experience, Extension, and another ComplexType which is DemoNestedLevelTwoand Finally this has StartDate and Salary columns. The column order can be changed by changing the property order in your model classes.

Excel file

Thanks for reading.

Full updated documentation can be found here on my GitHub Page - ExcelExport

Add a star to my repo if this saved you effort and time.

Share via LinkedIn, Twitter, Facebook, and WhatsApp to spread the knowledge.