ASP.NET MVC : Export data to CSV file | ASPMANTRA | Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server AND WCF snippets and tutorial

ASP.NET MVC : Export data to CSV file

In this article I am going to explain how to export data to CSV file using MVC.

I am showing list of movies. I want to export list of movies to CSV format. 

public partial class Tb_Movie
        public int Id { get; set; }
        public string Name { get; set; }
        public string Genre { get; set; }
        public Nullable<int> Budget { get; set; }

Add controller
Add empty controller to project. On index method write the code show the list of movies. After that create new FilecontentResult method to create and download CSV file.

Complete code of Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC_Project.Models;
using System.IO;

namespace MVC_Project.Controllers
    public class ExportExcelController : Controller
        DemoEntities1 db = new DemoEntities1();
        // GET: /ExportExcel/

        public ActionResult Index()
            var movielist = db.Tb_Movie.ToList();
            return View(movielist);

        public FileContentResult ExportToCSV()
            var movie = db.Tb_Movie.ToList();
             StringWriter sw = new StringWriter();
             sw.WriteLine("\"Id\",\"Movie Name\",\"Genre\",\"Budget\"");
             foreach (var mve in movie)
            var fileName = "Movielist" + DateTime.Now.ToString() + ".csv";
            return File(new System.Text.UTF8Encoding().GetBytes(sw.ToString()), "text/csv", fileName);


Add view
Now add view for index action.

Complete source of view:

@model IEnumerable<MVC_Project.Models.Tb_Movie>

    ViewBag.Title = "Export to CSV";
                      <div class="content">

                         @Html.ActionLink("ExportCSV", "ExportToCSV", "ExportExcel", new { @class = "btnexport" })
                                      @Html.DisplayNameFor(model => model.Name)
                                      @Html.DisplayNameFor(model => model.Genre)
                                      @Html.DisplayNameFor(model => model.Budget)

                              @foreach (var item in Model)
                                          @Html.DisplayFor(modelItem => item.Name)
                                          @Html.DisplayFor(modelItem => item.Genre)
                                          @Html.DisplayFor(modelItem => item.Budget)
                                          @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                                          @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                                          @Html.ActionLink("Delete", "Delete", new { id = item.Id })

    .btn {
        border: none;
        background: transparent;
    .content {
        min-height: 550px;
    a.btnexport {
        background: url(../images/Export-excel.png) no-repeat ;
        display: block;
        width: 150px;
        height: 45px;
        text-indent: -9999px;

Post a Comment



Contact Form


Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget