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.
Unfortunately, activation email could not send to your email. Please try again.

Exporting To Excel

Thread ID:

Created:

Updated:

Platform:

Replies:

128377 Jan 17,2017 03:41 PM Jan 19,2017 08:16 AM ASP.NET MVC 3
loading
Tags: Grid
Richard Dublon
Asked On January 17, 2017 03:41 PM

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");
        }


Prasanna Kumar Viswanathan [Syncfusion]
Replied On January 18, 2017 08:20 AM

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 


Richard Dublon
Replied On January 18, 2017 08:18 PM

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

Prasanna Kumar Viswanathan [Syncfusion]
Replied On January 19, 2017 08:16 AM

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 
 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;