In this article, we will explore how to convert a list of objects into a CSV (Comma-Separated Values) file in an ASP.NET MVC application using Entity Framework and SQL Server. This step-by-step guide includes fetching data from a SQL Server database, displaying it in an MVC view using Bootstrap for styling, and exporting the data to a CSV file from the controller.
Exporting data to CSV is a common feature in web applications, especially for reports and data management. This tutorial will walk you through the implementation of these functionalities.
Prerequisites
Before we begin, ensure you have the following:
- ASP.NET MVC project set up in Visual Studio
- SQL Server for the database
- Entity Framework installed
- Bootstrap for responsive UI design
Step 1: Setting Up the Database
We will start by creating a sample SQL Server database table to store student records.
CREATE TABLE [dbo].[Students] ( [Student_Id] INT IDENTITY(1,1) NOT NULL, [RollNo] INT NULL, [EnrollmentNo] NVARCHAR(15) NULL, [Name] NVARCHAR(50) NULL, [Branch] NVARCHAR(50) NULL, [University] NVARCHAR(50) NULL )
Now, insert sample data for demonstration purposes:
INSERT INTO Students (RollNo, EnrollmentNo, Name, Branch, University) VALUES (101, '14SOECE13017', 'Nikunj Satasiya', 'Computer Science', 'RK University'), (102, '14SOECE13018', 'Hiren Dobariya', 'Information Technology', 'RK University'), (103, '14SOECE13019', 'Vivek Ghadiya', 'Civil Engineering', 'RK University');
Step 2: Creating the ASP.NET MVC Project
Open Visual Studio and create a new ASP.NET MVC project.
Select MVC Template and name the project appropriately.
Add Entity Framework:
- Go to Solution Explorer > Right-click on the Models folder > Add > New Item.
- Select ADO.NET Entity Data Model and choose EF Designer from Database.
- Enter your SQL Server credentials and select the Students table.
Step 3: Writing the Controller Logic
In the HomeController.cs, add the following code:
using YourProject.Models; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using Microsoft.AspNetCore.Mvc; namespace YourProject.Controllers { public class HomeController : Controller { // Action to display student records public ActionResult Index() { using (YourDbContext db = new YourDbContext()) { var students = db.Students.ToList(); return View(students); } } // Action to export data to CSV [HttpPost] public FileResult ExportToCSV() { using (YourDbContext db = new YourDbContext()) { var students = db.Students.Select(s => new { s.RollNo, s.EnrollmentNo, s.Name, s.Branch, s.University }).ToList(); StringBuilder csv = new StringBuilder(); csv.AppendLine("RollNo,EnrollmentNo,Name,Branch,University"); foreach (var student in students) { csv.AppendLine($"{student.RollNo},{student.EnrollmentNo},{student.Name},{student.Branch},{student.University}"); } return File(Encoding.UTF8.GetBytes(csv.ToString()), "text/csv", "Students.csv"); } } } }
Step 4: Designing the View
Now, create the view for displaying student records. Navigate to Views/Home/Index.cshtml and add the following Razor code:
@model IEnumerable<YourProject.Models.Student> <h2>Student List</h2> <table class="table table-bordered"> <thead> <tr> <th>Roll No</th> <th>Enrollment No</th> <th>Name</th> <th>Branch</th> <th>University</th> </tr> </thead> <tbody> @foreach (var student in Model) { <tr> <td>@student.RollNo</td> <td>@student.EnrollmentNo</td> <td>@student.Name</td> <td>@student.Branch</td> <td>@student.University</td> </tr> } </tbody> </table> <form action="ExportToCSV" method="post"> <button type="submit" class="btn btn-success"> Export to CSV </button> </form>
Step 5: Running the Application
After completing the setup, run the project. Navigate to the student list view, and you should see the list of students displayed in a table. When you click on the "Export to CSV" button, the system will generate and download the CSV file with the student data.
Summary
In this tutorial, we demonstrated how to fetch records from a SQL Server database using Entity Framework in ASP.NET MVC and export those records to a CSV file. You learned how to:
- Set up a database and create a table
- Fetch data using Entity Framework
- Display data using Bootstrap in an MVC view
- Export data to a CSV file from the controller
By following this guide, you can easily extend the functionality to include more fields or different data models depending on your application’s needs.
Tags:
- ASP.NET MVC export CSV
- Entity Framework export data to CSV
- ASP.NET MVC file download
- C# convert object list to CSV