In a 'demo.cshtml' page of my C# MVC project, I'm displaying a list of users. I would like to export this data to an Excel file. Could someone assist me with this?
Loading
In a 'demo.cshtml' page of my C# MVC project, I'm displaying a list of users. I would like to export this data to an Excel file. Could someone assist me with this?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted May 9, 2024, 8:01 AM EDT
You can use the EPPlus library to easily export data to an Excel file. In your controller, create a method to generate the Excel file. This method will take the list of users as input and create an Excel file containing the user data.
using OfficeOpenXml; using System.IO; using System.Web.Mvc; public class UserController : Controller { public ActionResult ExportToExcel() { // Get the list of users from your data source List users = GetUsersFromDataSource(); ExcelPackage excelPackage = new ExcelPackage(); ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Users"); worksheet.Cells[1, 1].Value = "Name"; worksheet.Cells[1, 2].Value = "Email"; int row = 2; foreach (var user in users) { worksheet.Cells[row, 1].Value = user.Name; worksheet.Cells[row, 2].Value = user.Email; row++; } Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment; filename=Users.xlsx"); Response.BinaryWrite(excelPackage.GetAsByteArray()); Response.End(); return new EmptyResult(); } // Method to get users from your data source private List GetUsersFromDataSource() { return new List { new User { Name = "Abc", Email = "[email protected]" }, new User { Name = "Xyz", Email = "[email protected]" } }; } }Now add a button or link to trigger the export action. For example:
Jayraj ChhayaRank #304 · 8.2K reputationPosted May 9, 2024, 8:07 AM EDT
Hi Rinki,
To export data to an Excel file in a C# MVC project, you can use the EPPlus library, which simplifies Excel generation. First, install the EPPlus NuGet package in your project. Then, you can create an Excel package, add a worksheet, and populate it with your data.
using OfficeOpenXml; using System.IO; public void ExportToExcel(List users) { using (ExcelPackage excelPackage = new ExcelPackage()) { ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Users"); worksheet.Cells["A1"].LoadFromCollection(users, true); FileInfo excelFile = new FileInfo("Users.xlsx"); excelPackage.SaveAs(excelFile); } }ExportToExcelmethod takes a list of users and generates an Excel file named "Users.xlsx" with the user data. You can call this method from your controller to export the data.Ishika TiwariRank #298 · 8.4K reputationPosted May 9, 2024, 9:41 AM EDT
Hello Rinky,
Please go through this. I hope you will get your answer.
https://www.c-sharpcorner.com/article/how-to-export-html-table-in-excel-using-jquery/
Darshan AdakaneRank #344 · 7.3K reputationPosted May 9, 2024, 10:34 AM EDT
You can achieve this in two parts :
Part 1 :
using System.Collections.Generic; using System.Web.Mvc; using OfficeOpenXml; using System.IO; using YourNamespace.Models; // Import your user model namespace public class UserController : Controller { // Action method to export users to Excel public FileResult ExportUsersToExcel() { // Get list of users List users = GetUserList(); // Create Excel file using (ExcelPackage excelPackage = new ExcelPackage()) { // Add a worksheet ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Users"); // Set column headers worksheet.Cells[1, 1].Value = "Name"; worksheet.Cells[1, 2].Value = "Email"; // Populate data int row = 2; foreach (var user in users) { worksheet.Cells[row, 1].Value = user.Name; worksheet.Cells[row, 2].Value = user.Email; row++; } // Save Excel file to memory stream MemoryStream stream = new MemoryStream(); excelPackage.SaveAs(stream); // Set content type and file name string fileName = "Users.xlsx"; string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // Return Excel file as FileResult return File(stream.ToArray(), contentType, fileName); } } // Sample method to retrieve list of users (Replace with actual logic) private List GetUserList() { // Example: hardcoded list of users return new List { new User { Name = "John Doe", Email = "[email protected]" }, new User { Name = "Alice Smith", Email = "[email protected]" }, new User { Name = "Bob Johnson", Email = "[email protected]" } }; } }Part 2:
Hope this works. Thanks.
Jignesh KumarRank #91 · 22.9K reputationPosted May 9, 2024, 12:24 PM EDT
Hello Rinki,
Please refer to this article,
https://www.c-sharpcorner.com/article/export-data-in-excel-pdf-csv-word-json-xml-and-text-file-in-mvc-application/
https://www.c-sharpcorner.com/article/export-data-excel-in-asp-net-mvc/
https://www.c-sharpcorner.com/article/excel-export-in-mvc-net2/
Jaimin ShethiyaRank #177 · 13.7K reputationPosted May 9, 2024, 9:11 AM EDT
Hello Rinki,
Please refer below link.
https://docs.closedxml.io/en/latest/
https://github.com/ClosedXML/ClosedXML
https://www.c-sharpcorner.com/article/excel-sheet-closed-xml-mvc5/
Thanks