Controller:
public ActionResult MultipleExportTo(string[] GridModel, string typeExport) { //typeExport -> "xlsx" or "pdf" for (int i = 0; i < GridModel.Count(); i++)
...
First Grid
@(Html.EJ().Grid<object>("MasterGrid")
.Datasource((IEnumerable<object>)ViewBag.datasource1)
.SelectedRowIndex(0)
.AllowMultipleExporting()
.ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>
{
items.AddTool(ToolBarItems.ExcelExport);
items.AddTool(ToolBarItems.WordExport);
items.AddTool(ToolBarItems.PdfExport);
}))
.ClientSideEvents(eve =>
{
eve.ToolbarClick("toolbarClick");
})
.Columns(col =>
{
. . .
})
)
Second Grid
<div class="label1">
Orders Grid
</div>
@(Html.EJ().Grid<object>("DetailGrid")
.Datasource((IEnumerable<object>)ViewBag.datasource2)
.AllowPaging(false)
.Columns(col =>
{
. . .
})
)
function toolbarClick(args) {
var url = null;
if (args.itemName == "Excel Export") {
args.cancel = true;
url = "/Grid/MultipleExportToExcel";
}
if (args.itemName.indexOf("Export") != -1) {
$(".e-grid").ejWaitingPopup("show"); //show the waiting popup for all Grid
var firstGridModel = this.model, secondGridObject = $("#DetailGrid").data("ejGrid"), secondGridModel = secondGridObject.model, allModel = [JSON.stringify(firstGridModel), JSON.stringify(secondGridModel)];
var model = $.extend(true, [], allModel);
. . .
$.ajax({
type: "POST",
url: url,
data: { GridModel: model },//pass the grid model
dataType: "json",
success: function (response) {
$(".e-grid").ejWaitingPopup("hide");//hide the waiting popup for all Grid
alert("Grid Exported");
location.reload();
},
error: function (Result) {
$(".e-grid").ejWaitingPopup("hide"); //hide the waiting popup for all Grid
alert("Grid Exported");
location.reload();
}
}); }
}
@Control side
public object MultipleExportToExcel(string[] GridModel)
{
ExcelExport exp = new ExcelExport();
var EmployeeData = new NorthwindDataContext().EmployeeViews.Take(5).ToList();
var OrderData = new NorthwindDataContext().OrdersViews.Take(5).ToList();
bool initial = true;
IWorkbook book = null;
foreach (string gridProperty in GridModel)
{
GridProperties gridProp = ConvertObject(gridProperty);
if (initial)
{
gridProp.Locale = "";
book = exp.Export(gridProp, EmployeeData, "Export.xlsx", ExcelVersion.Excel2010, true, true, "flat-saffron", true);
initial = false;
}
else
{
exp.Export(gridProp, OrderData, "Export.xlsx", ExcelVersion.Excel2010, true, true, "flat-saffron", false, book, MultipleExportType.AppendToSheet, "Second Grid");
book.SaveAs(@"C:\Uploadfiles\Export.xlsx"); //save the file in local. Here you can specify the your own path
}
}
return Json(EmployeeData,JsonRequestBehavior.AllowGet);
}
|