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

Javascript | TreeGrid | Export to Excel with dynamic columns

Hello, 

I want to ask if is somehow possible to export treeGrid with dynamic columns. 

My following code throws an null poiner exception in exp.Export call:

Setup in JS:
---------------
                actionComplete: function (args) {
                    args.model.filteredRecords = TreeGridJs.DataSource;
                }
                treegridObj.export("api/ExportGridAsExcel");

Handle in C#:
---------------
    [RoutePrefix("api/ExportGridAsExcel")]
    public class ExportController : ApiController
    {
        //Excel Export Method.
        [HttpPost]
        [Route("")]
        public void ExcelExport()
        {
            string gridModel = HttpContext.Current.Request.Params["TreeGridModel"];
            GridProperties gridProperty = ConvertGridObject(gridModel);
            ExcelExport exp = new ExcelExport();

            exp.Export(gridProperty, (IEnumerable)gridProperty.DataSource, "Export.xlsx", ExcelVersion.Excel2010); //Here the System.NullReferenceException occurs, reffered in https://www.syncfusion.com/kb/3018/Null-Exception-while-exporting

        }

        //Grid Model conversion Method.
        private GridProperties ConvertGridObject(string gridProperty)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            IEnumerable div = (IEnumerable)serializer.Deserialize(gridProperty, typeof(IEnumerable));
            GridProperties gridProp = new GridProperties();

            foreach (KeyValuePair<string, object> ds in div)
            {
                if (ds.Key == "filteredRecords")
                {
                    string serialize = serializer.Serialize(ds.Value);
                    dynamic dynamicDataSource = new List<dynamic>();
                    var dataSource = serializer.Deserialize<IEnumerable<Dictionary<string, object>>>(serialize);
                    foreach (var dictionary in dataSource)
                    {
                        dynamic rowObject = new ExpandoObject();
                        foreach (var keyValuePair in dictionary)
                        {
                            ((IDictionary<String, Object>)rowObject).Add(keyValuePair.Key, keyValuePair.Value);
                        }
                        dynamicDataSource.Add((dynamic)rowObject);
                    }
                    gridProp.DataSource = dynamicDataSource;
                    continue;
                }
                var property = gridProp.GetType().GetProperty(ds.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                if (property != null)
                {
                    Type type = property.PropertyType;
                    string serialize = serializer.Serialize(ds.Value);
                    object value = serializer.Deserialize(serialize, type);
                    property.SetValue(gridProp, value, null);
                }
            }
            return gridProp;
        }
    }


3 Replies

MK Mahalakshmi Karthikeyan Syncfusion Team April 29, 2016 10:15 AM UTC

Hi Martin, 
Thanks for contacting Syncfusion support. 
At present there is no Excel Export support in TreeGrid control. But it is possible to export the Grid control to Excel. Can please provide us more information on your requirement? Do you want export support for TreeGrid control or Grid Control. Since you have using TreeGrid control in view page and processing the GridProperties in the Code behind. 
Regards, 
Mahalakshmi K. 



MR Martin R May 4, 2016 11:24 AM UTC

Hello Mahalakshmi K.,

we need support for exporting TreeGrid Control to Excel (Grid we used and we know that this functionality is here already). We also know that there is exporting to PDF for TreeGrid, but not to Excel format. I could imagine that the feature realization of the export to excel should send ajax request from client sending all information regarding the export (columns and datasource) to only pass it as parameters in WebApiController and get exported Excel.

Martin Rašovský.






JS Jonesherine Stephen Syncfusion Team May 5, 2016 11:52 AM UTC

Hi Martin, 
Thanks for contacting Syncfusion support.  
At present there is no support for Excel exporting in TreeGrid control. For this we have already logged a feature request regarding this. 
This feature will be available in our upcoming Volume 2 main release which is expected to be rolled out by the end of June 2016. 
Please let us know if you require further assistance on this. 
Regards, 
Jone Sherine P S 


Loader.
Live Chat Icon For mobile
Up arrow icon