<div id="Grid"></div>
<script type="text/javascript">
$(function () {
var data = @Html.Raw(Json.Encode(ViewBag.data));
$("#Grid").ejGrid({
dataSource: data,
. . . . . . .
toolbarClick: function(args) {
var url = null;
if (args.itemName == "Excel Export") {
args.cancel = true;
url = "/Home/ExportToExcel";
}
. . . . .
if (args.itemName.indexOf("Export") != -1) {
$("#Grid").ejWaitingPopup("show");
var model = $.extend(true, {}, this.model);
if (this.ignoreOnExport) {
for (var i = 0; i < this.ignoreOnExport.length; i++)
delete model[this.ignoreOnExport[i]];
}
$.ajax({
type: "POST",
url: url,
data: { GridModel: JSON.stringify(model) },//pass the grid model
dataType: "json",
success: function (response) {
$("#Grid").ejWaitingPopup("hide");
alert("Grid Exported");
},
error: function (Result) {
$("#Grid").ejWaitingPopup("hide");
alert("Error");
}
});
}
}
});
});
</script>
namespace Sample.Controllers
{
public partial class HomeController : Controller
{
. . . . .
. . . .
public ActionResult ExportToExcel(string GridModel)
{
GridProperties gridPropert = (GridProperties)Syncfusion.JavaScript.Utils.DeserializeToModel(typeof(GridProperties),GridModel);
ExcelExport exp = new ExcelExport();
IEnumerable data = new NorthwindDataContext().OrdersViews.ToList();
exp.Export(gridPropert, (IEnumerable)data, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-lime",true,Server.MapPath("/outPut"));
return Json(new List<string>(), JsonRequestBehavior.AllowGet);
}
. . . . .
}
}
|