Monday, June 26, 2017

ASP.NET MVC : Export data to Excel

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


Description:
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.

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 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()
        {
            try
            {
                var movie = db.Tb_Movie.ToList();
                GridView gv = new GridView();
                gv.DataSource = movie;
                gv.DataBind();
                Response.ClearContent();
                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);
                gv.RenderControl(htwriter);
                Response.Output.Write(swriter.ToString());
                Response.Flush();
                Response.End();
            }
            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")" />
                              </button>
                          }
                          <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;
    }
</style>

No comments:

Post a Comment