ASP.NET MVC : Export data to CSV file - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework 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 Asp.net MVC.


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

Model
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)
             {
                 sw.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\"",
                      mve.Id,
                                            mve.Name,
                                            mve.Genre,
                                            mve.Budget));
             }
            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" })
                          <table>
                              <tr>
                                  <th>
                                      @Html.DisplayNameFor(model => model.Name)
                                  </th>
                                  <th>
                                      @Html.DisplayNameFor(model => model.Genre)
                                  </th>
                                  <th>
                                      @Html.DisplayNameFor(model => model.Budget)
                                  </th>
                                  <th></th>
                              </tr>

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

                          </table>
                      </div>
<style>
    .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;
    }
</style>






What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+


EmoticonEmoticon