Updated on Kisan Patel
How to Export and Download Excel files with .NET Core?
First, add the EPPlus into your project:
dotnet add package EPPlus
Now, generate the Excel file using EPPlus.
using OfficeOpenXml; using System.IO; namespace MyCMS.Controllers { public class HomeController : ControllerBase { [HttpGet("export")] public async TaskExport() { //fetch list using linq query var list = db.list.ToList(); var stream = new MemoryStream(); using (var package = new ExcelPackage(stream)) { var workSheet = package.Workbook.Worksheets.Add("sheetName"); workSheet.Cells.LoadFromCollection(list, true); package.Save(); }; stream.Position = 0; var contentType = "application/octet-stream"; var fileName = "fileName.xlsx"; return File(stream, contentType, fileName); } } }