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

ASP.NET MVC : Export data to Excel

In this article I am going to explain how to export data to Excel file in MVC.

I have created a table Tb_Movie and insert some dummy data into it. I am showing the list of movies and want to export data to Excel file.


   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 a controller to project. On index method write the code get the list of movies. After that create a method to export the data.

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.Web.UI.WebControls;
using System.IO;
using System.Web.UI;

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 ActionResult ExportToExcel()
                var movie = db.Tb_Movie.ToList();
                GridView gv = new GridView();
                gv.DataSource = movie;
                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment; filename=Movie_List.xlsx");
                Response.ContentType = "application/ms-excel";
                Response.Charset = "";
                StringWriter swriter = new StringWriter();
                HtmlTextWriter htwriter = new HtmlTextWriter(swriter);
            catch (Exception ex) { }
            return View();

Add View
Add view for index method.

Complete source of Index.cshtml

@model IEnumerable<MVC_Project.Models.Tb_Movie>

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

                          @using (Html.BeginForm("ExportToExcel", "ExportExcel", FormMethod.Post))
                              <button class="btn">
                                  <img src="@Url.Content("~/images/Export-excel.png")" />
                                      @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;

Post a Comment



Contact Form


Email *

Message *

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