Wednesday 22 October 2014

How to export WebGrid to Excel in Asp.Net Mvc 4.0

How to export WebGrid to Excel in Asp.Net Mvc 4.0......................

Note: In this topic I will explain  How to Export Webgrid to Excel in asp.net MVC4 Application.

-- Sql Query.....................................
--In this table TotalMarks Column is computed column

create table StudentMarks
(
RollNo int primary key,
Name varchar(50),
Physics int,
Chemistry int,
Maths int,
TotalMarks  as ((Physics+Chemistry)+Maths )
)

First we will open Visual Studio 2012 AFTER THAT following below steps
Step 1: File -> New Project -> ASP.NET MVC 4 Web Application
And select Internet Application with Razor View engine (as like below)



Step 2: After that we will add Model1.edmx in Model like…..



Step 2: Next step we will add Controllerwhich name is WebGridToExcel and  write code 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using this namespace.....
using System.Web.Helpers;
using System.Web.Mvc;
using WebGridConceptInMvc4.Models;

namespace WebGridConceptInMvc4.Controllers
{
public class WebGridToExcelController : Controller
{
//
// GET: /WebGridToExcel/

public ActionResult Index()
{
return View();
}
//  ADD NEW ACTION INTO YOUR CONTROLLER FOR DATA IN WEBGRID.
public ActionResult StudentMarksDetails()
{

List<StudentMark> stumarks = new List<StudentMark>();
using (testEntities tt = new testEntities())
{
stumarks = tt.StudentMarks.OrderBy(m => m.RollNo).ToList();

}
return View(stumarks);
}

ADD VIEW FOR for above ACTION & DESIGN with sTRONGLY tYPED vIEW

@model List<WebGridConceptInMvc4.Models.StudentMark>

@{
Layout = null;
ViewBag.Title = "List of Students";
var grid = new WebGrid(source: Model, canPage: true, rowsPerPage: 10);
grid.Pager(WebGridPagerModes.All);
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Student Marks Details</title>
<style type="text/css">
.tablestyle
{

font-family:Verdana;
font-size: 0.8em;
width: 70%;
display: table;
border-collapse:collapse;
border: solid 2px Black;
background-color:white;


}

.tablestyle td, th
{
border: 1px solid #E022C2;
padding: 2px 2px 2px;
}
.wgheader
{
background-color:white ;
color:#ff6a00;
padding-bottom: 4px;
padding-top: 5px;
text-align: left;

}

.dob
{
color:green;
background-color:lightpink;
}
</style>
</head>
<body>
<table width="90%">
<tr><td>How to export WebGrid to Excel in Asp.Net Mvc 4.0 </td></tr>
<tr><td>

<div id="mydata">
@grid.GetHtml(
tableStyle:"tablestyle",
headerStyle:"wgheader",
columns:grid.Columns
(
grid.Column(columnName:"RollNo",header:"RollNo"),
grid.Column(columnName:"Name",header:"Name"),
//you Can also apply format and any Css property with any column

grid.Column("Dob",header:"Date of Birth",format: @<text>@item.Dob.ToString("dd-MMM-yyyy HH:mm")</text>,style: "dob",canSort:false),

grid.Column(columnName:"Physics", header:"Physics"),
grid.Column(columnName:"Chemistry", header:"Chemistry"),
grid.Column(columnName:"Maths", header:"Maths"),
grid.Column(columnName:"TotalMarks", header:"Total Marks",canSort:false)

))
</div>
</td></tr>
<tr><td>Export Data :  @Html.ActionLink("Get Excel in Download Folder","GetExcel","WebGridToExcel")</td></tr>
@*with Action Link (string name, Method Name,Controller Name)*@
</table>

</body>
</html>




//Add Another Method Into Same Controller For Export Webgrid in Excel Format.

public void GetExcel()
{
List<StudentMark> stumarks = new List<StudentMark>();
using (testEntities tt = new testEntities())
{
stumarks = tt.StudentMarks.OrderBy(m => m.RollNo).ToList();

}
WebGrid wd = new WebGrid(source: stumarks, canPage: false, canSort: false);


string griddata = wd.GetHtml(
columns: wd.Columns
(
wd.Column(columnName: "RollNo", header: "RollNo"),
wd.Column(columnName: "Name", header: "Name"),
wd.Column(columnName: "Dob", header: "Dob"),
wd.Column(columnName: "Physics", header: "Physics"),
wd.Column(columnName: "Chemistry", header: "Chemistry"),
wd.Column(columnName: "Maths", header: "Maths"),
wd.Column(columnName: "TotalMarks", header: "Total Marks")

)
).ToString();

Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=StudentMarksDetails.xls");
Response.ContentType = "application/excel";
Response.Write(griddata);
Response.End();

}
}
}


Result


0 comments:

Post a Comment