:::: MENU ::::

Export Excel and CSV file in Asp.Net Core

Last week I was working on exporting data to Excel and CSV format in AspNet Core and I want to share how I did it.

Sample Code: https://github.com/janaks09/ExcelCSVExportInAspNetCore/

Export to Excel:

To build Excel file, I am using EPPlus.Core package which is an unofficial port of EEPlus library to .NET Core. Now EPPlus has released beta package which supports .NET Core 2.0 check that too. https://github.com/JanKallman/EPPlus/issues/46

Steps:

  1. Install EEPlus.Core package from Nuget
  2. Build excel package
  3. Download as a file

Here is the Code:

 

This is where you are defining the column length (no. of a column in your sheet). Then you need to parse your data and set right value in right cell. To export to an excel file I am using File method of MVC and by defining content type and filename you are ready to initiate a download.

Export to CSV:

To build and export CSV file you don’t need third party library.

Steps:

  1. Convert your data to comma separated string
  2. Convert Column header/title in comma separated value as well
  3. Convert it into stream or byte array
  4. Download as File

Here is a Code:

Nothing complex here. If your data have “,” in content then you need to escape that value/content using “[VALUE]” which I have shown above. And Content-Type of CSV file is text/csv.

That’s all. This is one way on how you can export excel or csv file in aspnet core.

Happy Coding😊