Simple classes to generate Excel/CSV Report in ASP.NET Core.
To export/download the IEnumerable<T>
data as an excel file, add action method in your controller as shown below. Return the data as ExcelResult<T>
/CSVResult<T>
by passing filtered/ordered data, sheet name and file name. ExcelResult/CSVResult Action Result that I have added in the Nuget package. This will take care of converting your data as excel/csv file and return it back to browser.
Note: This tutorial contains example for downloading/exporting excel/csv from Asp.Net Core Backend.
If you liked ExcelExport
project or if it helped you, please give a star ⭐️ for this repository. That will not only help strengthen our .NET community but also improve development skills for .NET developers in around the world. Thank you very much 👍
Column names in Excel Export can be configured using the below attributes
[Display(Name = "")]
[DisplayName(“”)]
To customize the Excel Column display in your report, use the following attribute
[IncludeInReport]
[NestedIncludeInReport]
And here are the options,
Option | Type | Example | Description |
---|---|---|---|
Order | int |
[IncludeInReport(Order = N)] |
To control the order of columns in Excel Report |
Please note: From v.2.0.0, simple properties in your models with [IncludeInReport]
attribute will be displayed in excel report. You can add any level of nesting to your models using [NestedIncludeInReport]
attribute.
- Fingers10.ExcelExport v3.0.0
PM> Install-Package Fingers10.ExcelExport
> dotnet add package Fingers10.ExcelExport
public class DemoExcel
{
public int Id { get; set; }
[IncludeInReport(Order = 1)]
public string Name { get; set; }
[IncludeInReport(Order = 2)]
public string Position { get; set; }
[Display(Name = "Office")]
[IncludeInReport(Order = 3)]
public string Offices { get; set; }
[NestedIncludeInReport]
public DemoNestedLevelOne DemoNestedLevelOne { get; set; }
}
public class DemoNestedLevelOne
{
[IncludeInReport(Order = 4)]
public short? Experience { get; set; }
[DisplayName("Extn")]
[IncludeInReport(Order = 5)]
public int? Extension { get; set; }
[NestedIncludeInReport]
public DemoNestedLevelTwo DemoNestedLevelTwos { get; set; }
}
public class DemoNestedLevelTwo
{
[DisplayName("Start Date")]
[IncludeInReport(Order = 6)]
public DateTime? StartDates { get; set; }
[IncludeInReport(Order = 7)]
public long? Salary { get; set; }
}
public async Task<IActionResult> GetExcel()
{
// Get you IEnumerable<T> data
var results = await _demoService.GetDataAsync();
return new ExcelResult<DemoExcel>(results, "Demo Sheet Name", "Fingers10");
}
public async Task<IActionResult> GetCSV()
{
// Get you IEnumerable<T> data
var results = await _demoService.GetDataAsync();
return new CSVResult<DemoExcel>(results, "Fingers10");
}
public async Task<IActionResult> OnGetExcelAsync()
{
// Get you IEnumerable<T> data
var results = await _demoService.GetDataAsync();
return new ExcelResult<DemoExcel>(results, "Demo Sheet Name", "Fingers10");
}
public async Task<IActionResult> OnGetCSVAsync()
{
// Get you IEnumerable<T> data
var results = await _demoService.GetDataAsync();
return new CSVResult<DemoExcel>(results, "Fingers10");
}
- .Net Standard 2.0
- Visual Studio Community 2019
- ClosedXML (0.95.0) - For Generating Excel Bytes
- Microsoft.AspNetCore.Mvc.Abstractions (2.2.0) - For using IActionResult
- System.ComponentModel.Annotations (4.7.0) - For Reading Column Names from Annotations
- Abdul Rahman - Software Developer - from India. Software Consultant, Architect, Freelance Lecturer/Developer and Web Geek.
Feel free to submit a pull request if you can add additional functionality or find any bugs (to see a list of active issues), visit the Issues section. Please make sure all commits are properly documented.
ExcelExport is release under the MIT license. You are free to use, modify and distribute this software, as long as the copyright header is left intact.
Enjoy!
I'm happy to help you with my Nuget Package. Support this project by becoming a sponsor/backer. Your logo will show up here with a link to your website. Sponsor/Back via