Monday 30 December 2013

MVC Example for Export or transfer Gridview Data to Excelsheet File in MVC4





Description:-

            In previous Example we Explain that How to Export Gridview data to Excelsheet in Asp.Net  Export Gridview data to excelsheet in asp.net but now in this example we explain that how to Export Gridview Data to Excel sheet in MVC4.

This is very useful when we have to generally Create a Report or you can Directally print the Document

you have to set Content Type of the page Before sending or Exporting data to ExcelSheet like
Response.ContentType = "application/ms-excel";

to show example of WCF service for insert,update,delete CRUD operation using WCF service

check the Extention of file when upload upload only image or word file

there are number of steps to follow how to Export or transfer Gridview or webgird data to Excelsheet Database file.

step 1:- First create a LinqtoSQL class in Model folder and drag and srop the table that you have to use in example.

step 2:- create a controller like exportgridtoexcel in controller Folder.

step 3:- create a view page For display the details we have to create a view page for that in the exportgridtoexcel controller add following code.



   DataClasses1DataContext dc = new DataClasses1DataContext();
        public ActionResult Index()
        {
            var lst = (from p in dc.movies select p).ToList();
            return View(lst);
        }

step 4:-Then for export the data to excel add following code to the exportgridtoexcel Controller



   public List<movie> fetch()
        {
             var lst = (from p in dc.movies select p);
            return lst.ToList();
        }

        public ActionResult ExportData()
        {
            GridView gv = new GridView();
            gv.DataSource =fetch();
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=Marklist.xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

            return RedirectToAction("Index");
        }






exportgridtoexcel:-



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
using System.Data;
namespace MvcApplication1.Controllers
 
{
    public class exportgridtoexcelController : Controller
    {
      
        DataClasses1DataContext dc = new DataClasses1DataContext();
        public ActionResult Index()
        {
            var lst = (from p in dc.movies select p).ToList();
            return View(lst);
        }
        public List<movie> fetch()
        {
             var lst = (from p in dc.movies select p);
            return lst.ToList();
        }
        public ActionResult ExportData()
        {
            GridView gv = new GridView();
            gv.DataSource =fetch();
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=Marklist.xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

            return RedirectToAction("Index");
        }

      
    }
}


Index.cshtml:-

@model IEnumerable<MvcApplication1.Models.movie>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
  
</p>


<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.moviedesc)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.moviedesc)
            </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>
 @using (Html.BeginForm("ExportData", "exportgridtoexcel", FormMethod.Post, new { enctype = "multipart/form-data" }))
         {
        <table>
         <tr><td></td><td><input  type="submit" name="Export" id="Export" value="Export"/></td></tr>
       
        </table>
         }

 
now add the link in layout.cshtml page like



<li>@Html.ActionLink("exportgridtoexcel", "Index", "exportgridtoexcel")</li>


0 comments:

Post a Comment