@{Html.EJ().Grid<object>("Grid")
.Datasource((IEnumerable<object>)ViewBag.data)
.AllowPaging()
.ToolbarSettings(toolbar =>
{
toolbar.ShowToolbar()
.ToolbarItems(items =>
{
items.AddTool(ToolBarItems.ExcelExport);
items.AddTool(ToolBarItems.PdfExport);
items.AddTool(ToolBarItems.WordExport);
});
})
//Collect the ViewBag columns and assign them to the Grid Columns
.Columns(ViewBag.columns)
.Render();
}
public class HomeController : Controller
{
public static List<Orders> order = new List<Orders>();
public static List<Column> cols = new List<Column>();
public ActionResult Index()
{
. . .
. . .
/* Add Grid Columns
* intial Load of the Page
* Save it in Global and static Variable
* Assign them to ViewBag
*/
cols.Add(new Column() { Field = "OrderID", HeaderText = "Order ID" });
cols.Add(new Column() { Field = "CustomerID", HeaderText = "Order ID" });
cols.Add(new Column() { Field = "EmployeeID", HeaderText = "Order ID" });
cols.Add(new Column() { Field = "Freight", HeaderText = "Order ID" });
ViewBag.columns = cols;
return View();
}
public void addColumns(GridProperties gridObj){
if (cols.Count == gridObj.Columns.Count)
cols.Add(new Column() { Field = "ShipCity", HeaderText = "Ship City" });
gridObj.Columns = cols;
}
public void ExportToExcel(string GridModel)
{
ExcelExport exp = new ExcelExport();
var DataSource = order;
GridProperties obj = ConvertGridObject(GridModel);
//Push the additional columns before exporting the Grid.
addColumns(obj);
exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
}
. .
. .
} |