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,
Package Manager
.Net CLI
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 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

  1. public class DemoExcel  
  2. {  
  3.     public int Id { getset; }  
  5.     public string Name { getset; }  
  7.     public string Position { getset; }  
  9.     [Display(Name = "Office")]  
  10.     public string Offices { getset; }  
  12.     public DemoNestedLevelOne DemoNestedLevelOne { getset; }  
  13. }  

Nested Level One Model

  1. public class DemoNestedLevelOne  
  2. {  
  3.     public short? Experience { getset; }  
  5.     [DisplayName("Extn")]  
  6.     public int? Extension { getset; }  
  8.     public DemoNestedLevelTwo DemoNestedLevelTwos { getset; }  
  9. }  

Nested Level Two Model

  1. public class DemoNestedLevelTwo  
  2. {  
  3.     [DisplayName("Start Date")]  
  4.     public DateTime? StartDates { getset; }  
  6.     public long? Salary { getset; }  
  7. }  
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 service and then return it as an ExcelResult<T> by specifying the Root Model and sheet name and Excel file name. 
  1. public async Task<IActionResult> GetExcel()  
  2. {  
  3.     // Get you IEnumerable<T> data  
  4.     var results = await _demoService.GetDataAsync();  
  5.     return new ExcelResult<DemoExcel>(results, "Demo Sheet Name""Fingers10");  
  6. }  
Page Handler
The same goes for page handlers. Now in your page handler, get your IEnumerable<T> data from service and then return it as an ExcelResult<T> by specifying the Root Model and sheet name and Excel file name.
  1. public async Task<IActionResult> OnGetExcelAsync()  
  2. {  
  3.      // Get you IEnumerable<T> data  
  4.     var results = await _demoService.GetDataAsync();  
  5.     return new ExcelResult<DemoExcel>(results, "Demo Sheet Name""Fingers10");  
  6. }  
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 inturn 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 Export/Download In ASP.NET Core
Thanks for reading.
  • Full updated documentation can be found here in 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.