In this article I will explain with an example, how to export Partial View to
Excel file in ASP.Net MVC Razor.
First the Grid (Html Table) will be populated from database using
Entity Framework inside the Partial View and then the HTML Table will be sent as HTML string to the Controller which ultimately will be exported to
Excel file in ASP.Net MVC Razor.
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
Entity Framework Model
Once the
Entity Framework is configured and connected to the database table, the Model will look as shown below.
Namespaces
You will need to import the following namespace.
Controller
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, the Top 10 Customer records are fetched and returned to the View.
Action method for handling the Excel File Export and Download operation
This Action method is executed when the Export Submit button is clicked.
Note: The following Action method performs File Download and hence the return type is set to FileResult.
The HTML of the Grid sent from the View is extracted from the GridHtml parameter.
The HTML string is converted into Byte Array using the GetBytes functions of the ASCII class.
Finally it is exported and downloaded as
Excel file using the
File function.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
NorthwindEntities entities = new NorthwindEntities();
return View(from customer in entities.Customers.Take(10)
select customer);
}
[HttpPost]
[ValidateInput(false)]
public FileResult Export(string GridHtml)
{
return File(Encoding.ASCII.GetBytes(GridHtml), "application/vnd.ms-excel", "Grid.xls");
}
}
View
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that it will be available as a Collection.
For displaying the records, the Partial View is rendered using @Html.Partial function which renders the Partial View using Model.
The Partial View will render as an HTML Grid (Table) consisting of the records from the Customers Table.
There is an HTML Hidden Field element which is used to send the HTML of the Partial View’s Grid (Html Table) content to the Controller’s Action method.
Finally there’s an HTML Submit button enclosed inside a Form with the Action method specified as Export.
When this Button will be clicked, first the HTML of the Partial View’s Grid (Html Table) is extracted and set into the Hidden Field element and finally the Form is submitted.
@model IEnumerable<Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
<h4>Customers</h4>
<hr/>
<div id="Grid">
@Html.Partial("Grid_View", Model)
</div>
<br/>
<br/>
@using (Html.BeginForm("Export", "Home", FormMethod.Post))
{
<input type="hidden" name="GridHtml"/>
<input type="submit" id="btnSubmit" value="Export"/>
}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btnSubmit").click(function () {
$("input[name='GridHtml']").val($("#Grid").html());
});
});
</script>
</body>
</html>
Partial View
Inside the Partial View, in the very first line the Customer Entity is declared as IEnumerable which specifies that it will be available as a Collection.
For displaying the records, an HTML Table is used. A loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
@model IEnumerable<Customer>
<table cellpadding="5" cellspacing="0" style="border: 1px solid #ccc;font-size: 9pt;border-collapse:collapse">
<tr>
<th style="background-color: #B8DBFD;border: 1px solid #ccc">CustomerID</th>
<th style="background-color: #B8DBFD;border: 1px solid #ccc">ContactName</th>
<th style="background-color: #B8DBFD;border: 1px solid #ccc">City</th>
<th style="background-color: #B8DBFD;border: 1px solid #ccc">Country</th>
</tr>
@foreach (Customer customer in Model)
{
<tr>
<td style="width:120px;border: 1px solid #ccc">@customer.CustomerID</td>
<td style="width:120px;border: 1px solid #ccc">@customer.ContactName</td>
<td style="width:120px;border: 1px solid #ccc">@customer.City</td>
<td style="width:120px;border: 1px solid #ccc">@customer.Country</td>
</tr>
}
</table>
Screenshots
Grid (Html Table)
Exported Excel File
Downloads