This article will show you how you can asp.net mvc export
data to excel file of webgrid using c#.net. So in this article first we will
bind grid view by using entity
framework.
Some of my previous articles are as follows: How
to Bind Data to Webgrid in ASP.net MVC Using C#.Net, Responsive
Grid Design Example Using Css in Asp.Net MVC,HTML, Bind
jQuery DatePicker Calendar In MVC WebGrid and Retrive Value Using Asp.net MVC,
C#.Net, Bind
DropDownList In MVC WebGrid and Retrive Value Using Asp.net MVC, C#.Net, Add
Textbox In WebGrid and Access TextBox Value In Controller In Asp.net MVC Using
C#.Net, FileUpload
Control Inside WebGrid To Upload File In Asp.net MVC Using C#.Net, Frozen
Rows and Columns in Asp.Net Mvc Webgrid Using jQuery Like Excel Sheet.
So for this article we will create a new asp.net mvc
application. So before adding writing the view code we will create the model
class file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1.Models
{
public class ProductDetailModel
{
public
List<Product>
ProductList { get; set;
}
}
public class Product
{
public
int ID { get;
set; }
public
string ProductName { get;
set; }
public
string ProductDetail { get; set; }
public
int CurrentStock { get;
set; }
}
}
|
Now we will add an entity file and add the table.
Now we will create a view of controller action and add the
blow code into the view of the page.
@model MvcApplication1.Models.ProductDetailModel
@{
ViewBag.Title = "Asp.Net
MVC Export Data to Excel File Of WebGrid Using C#.Net";
}
<style>
table, td, th
{
border:
1px solid green;
border-collapse:
collapse;
}
th
{
border:
1px solid black;
background-color:
green;
color:
white;
}
</style>
@using (@Html.BeginForm("Index", "Home"))
{
var grid
= new WebGrid(Model.ProductList,
canSort: false);
<div>
@grid.GetHtml(columns:
grid.Columns
(
grid.Column("ID", "ID"),
grid.Column("ProductName", "Product Name"),
grid.Column("ProductDetail", "Product Detail"),
grid.Column("CurrentStock", "Current Stock")
), mode: WebGridPagerModes.Numeric)
</div>
<div>
<input type="submit" value="Exxport Data" />
</div>
}
|
After this we will add the below code into the controller of
the page.
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;
namespace MvcApplication1.Controllers
{
public class HomeController
: Controller
{
//
// GET:
/Home/
[HttpGet]
public
ActionResult Index()
{
ProductDetailModel
objproductdetailmodel = new ProductDetailModel();
objproductdetailmodel.ProductList
= ProductData();
return
View(objproductdetailmodel);
}
[HttpPost]
public
ActionResult Index(int id = 0)
{
ProductDetailModel
objproductdetailmodel = new ProductDetailModel();
objproductdetailmodel.ProductList
= ProductData();
/*Binding
product detail*/
var
dataGrid = new GridView();
dataGrid.DataSource =
objproductdetailmodel.ProductList;
dataGrid.DataBind();
/*Code
to eport the detail in excel sheet format*/
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=ProductExcelFile.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter
sw = new StringWriter();
HtmlTextWriter
htw = new HtmlTextWriter(sw);
dataGrid.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return
View(objproductdetailmodel);
}
///
/// This finction is used for providing the data.
/// we will bind this data
/// to diaply in grid format.
/// You can get data from data base and
/// thee put into collection
///
public List<Product> ProductData()
{
List<Product> objProduct = new List<Product>();
DemoEntities objDemoEntities = new DemoEntities();
var productItem = from data in objDemoEntities.ProductMasters
select data;
foreach (var item in productItem)
{
objProduct.Add(new Product
{
ID = item.Id,
ProductName = item.ProductName,
ProductDetail = item.ProductDetail,
CurrentStock = (int)item.CurrentStock
});
}
return objProduct;
}
}
}
|
In above I have fetched the data from the database and made the collection. After that on button
click I have create the gridview variable and bind the list data to the grid view.
After that I have code of export code have been written.
Now we have done run the application and check the output.
Good Post.
ReplyDeleteYou are great.Thank You.
Good Post.
ReplyDeleteYou are great.Thank You.
What is DemoEntities ? Its showing error over there...
ReplyDeleteGood post, thanks!
ReplyDeleteI've noticed that fields that have leading zeroes (001001) end up losing them when they end up in Excel (1001). Any thoughts on how to avoid that?
this is really good post but how to rename excel header text in mvc..
ReplyDeleteThanks for your valuable comment. I will check and get back to you for header rename
Delete