Thursday, 23 October 2014

How to Export WebGrid to Pdf in asp.net MVC4 Application

How to Export WebGrid to Pdf in asp.net MVC4 Application………………..

Note: In this topic I will explain How to Export WebGrid to Pdf 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 3: Next step we will add Controller which name is WebGridToPdf and  write code 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
// using these namespace here
using System.Web.Helpers;
using iTextSharp.text;
using iTextSharp.text.pdf;
using WebGridConceptInMvc4.Models;

namespace WebGridConceptInMvc4.Controllers
{
public class WebGridToPdfController : Controller
{
//
// GET: /WebGridToPdf/

public ActionResult Index()
{
return View();
}
//Add new Action into your Controller for data in WebGrid
public ActionResult StudentMarksDetails()
{

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

}
return View(stumarks);
}

}
}

Add View for above Action and Design with Strongly typed view like



Code for  StudentMarksDetails.cshtml................

@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>Export WebGrid to PDF</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 PDF 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 PDF ","GetPdf","WebGridToPdf")</td></tr>
@*with Action Link (string name, Method Name,Controller Name)*@
</table>

</body>
</html>

Before going to add Action for   get PDF to WebGrid we need to add ItextSharp .ddl  and add ItextSharpxmlworker .ddl in our project

How  to  add both ddl in our project given below  from Internet


Here I have added "GetPdf" Action into "WebGridToPdf" Controller. Please write this following code import following...

// How to convert WebGrid to Pdf…………………………..

public FileStreamResult GetPdf()
{
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();

string exportData = String.Format("<html><head>{0}</head><body>{1}</body></html>", "<style>table{ border-spacing: 10px; border-collapse: separate; }</style>",griddata);
var bytes = System.Text.Encoding.UTF8.GetBytes(exportData);
using (var input = new MemoryStream(bytes))
{
var output = new MemoryStream();
var document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);
var writer = PdfWriter.GetInstance(document, output);
writer.CloseStream = false;
document.Open();

var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF8);
document.Close();
output.Position = 0;
return new FileStreamResult(output, "application/pdf");

}
}

Result:

2 comments: