We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Exporting To Excel

Hi,
I am trying to export my grid to excel but when I click on the button in the toolbar, my application tries to load a view called ExportToExcel that doesn't exist.  
How can I get it to call the export method that is in the current views controller?

Here is what my grid looks like:

@(Html.EJ().Grid<DKDatabaseModelClass.GrossLeadsByStateModel>("MasterGrid")
                    .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>
                    {
                        items.AddTool(ToolBarItems.ExcelExport);
                    }))
                   .ClientSideEvents(events => events.Load("onLoad").DataBound("dataBound"))
                   .Datasource((IEnumerable<object>)ViewBag.datasource)
                   .AllowPaging()
                   .AllowSorting()
                   .AllowFiltering()
                   .EnableAltRow()
                   .ShowSummary()
    .SummaryRow(row =>
    {
        row.Title("Count").SummaryColumns(col => { col.SummaryType(SummaryType.Sum).DisplayColumn("LeadCount").DataMember("LeadCount").Add(); }).Add();
    })
        .Columns(col =>
        {
            col.Field("State").HeaderText("State").Add();
            col.Field("MailDate").HeaderText("Mail Date").Format("{0:M/d/yyyy}").Add();
            col.Field("LeadCount").HeaderText("Lead Count").Add();


        })
        .ClientSideEvents(eve => { eve.RowSelected("rowSelected"); })
    )

Here is what is in my controller:

        public void ExportToExcel(string GridModel)
        {
            ExcelExport exp = new ExcelExport();
            var DataSource = LeadsByStateMailDate();
            GridProperties obj = ConvertGridObject(GridModel);
            exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
        }


3 Replies

PK Prasanna Kumar Viswanathan Syncfusion Team January 18, 2017 01:20 PM UTC

Hi Richard, 

Thanks for contacting Syncfusion support. 

In Exporting, the default routing path to server-side is the ExportToExcel method in current path/controller for Excel Exporting. For instance, when Grid is rendered in GridFeatures View Page of Home Controller, then on Excel exporting Grid Content, the default routing path is ~/Home/ExportToExcel. 
 
We suspect that the reported issue arises due to routing path and so we suggest you to use Mappers grid property. By using Mappers, you can use any action name in Controller for exporting and the action can be in any controller (Need not to be in Grid View Page Controller). 
 
For more information refer the below link: 
 

Regards, 
Prasanna Kumar N.S.V 



RD Richard Dublon January 19, 2017 01:18 AM UTC

That worked great for my master grid but not for my detail one.
I have a master detail grid set up but when I try to export with my detail grid, I get the following error:

     Unable to cast object of type 'System.Web.Mvc.JsonResult' to type 'System.Collections.IEnumerable'.

My detail grids data is loaded with the following javascript:

      var state = value.data.State;    
      var maildate = value.data.maildate; 
      var datasource = data;
      var detaildata = ej.DataManager(data).executeLocal(ej.Query().where("State", ej.FilterOperators.equal, state, false));
      var gridObj = $("#DetailGrid").ejGrid("instance");
      gridObj.dataSource(ej.DataManager(detaildata));

My export code in the controller looks like this:

        public void ExportDetailGrid(string GridModel)
        {
            ExcelExport exp = new ExcelExport();
            var DataSource = LeadsByStateCountyMailDate() ;
            GridProperties obj = ConvertGridObject(GridModel);
            exp.Export(obj, DataSource, "LeadsByStateCountyMailDate.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
        }

My LeadsByStateCountyMailDate() is this:

        public JsonResult LeadsByStateCountyMailDate()
        {
            var dkContext = new DKDatabaseEntities();

            var result = (from m in dkContext.Mailings
                          join p in dkContext.Persons on m.ID equals p.MailingID
                          join a in dkContext.Addresses on p.ID equals a.PersonID
                          where m.NoMail != "T"
                          group a by new { a.State, m.MailDate, a.County } into gg
                          orderby gg.Key

                          select new DKDatabaseModelClass.GrossLeadsByStateCountyModel { MailDate = gg.Key.MailDate.ToString(), State = gg.Key.State, County = gg.Key.County, LeadCount =          
                          gg.Count() }).ToList();

            //ViewBag.datasource = result.ToList();
            return Json(result, JsonRequestBehavior.AllowGet);
        }

If I change my method from type JsonResult to a List like the method that populates my master grid, my detail grid does not populate.

Thanks


PK Prasanna Kumar Viswanathan Syncfusion Team January 19, 2017 01:16 PM UTC

Hi Richard, 

We can reproduce the mentioned issue with the mentioned code example. In your code example we found that you have passed JsonResult as a dataSource in the export method. In export method we need to pass the IEnumerable data as a dataSource.  

Find the code example :  


public void ExportDetailGrid(string GridModel) 
        { 
            ExcelExport exp = new ExcelExport(); 
            var DataSource = LeadsByStateCountyMailDate(); 
            GridProperties obj = ConvertGridObject(GridModel); 
            exp.Export(obj, (IEnumerable)DataSource.Data, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron"); 
        } 
        public JsonResult LeadsByStateCountyMailDate() 
        { 
            var dkContext = new NorthwindDataContext(); 
 
            var result = dkContext.OrdersViews.ToList(); 
 
            //ViewBag.datasource = result.ToList(); 
            return Json(result, JsonRequestBehavior.AllowGet); 
        } 


Regards, 
Prasanna Kumar N.S.V 
 


Loader.
Live Chat Icon For mobile
Up arrow icon