Category / Section
How to export client-side data in TreeGrid
1 min read
In TreeGrid, it is possible to export the client-side data. This can be achieved by convert the TreeGrid data source to JSON string and send it as new property to the TreeGrid model before calling the export method.
CSHTML
<script> function exportPDF() { var treeObj = $("#TreeGridContainer").ejTreeGrid("instance"); treeObj.model["exportData"] = JSON.stringify(treeObj.dataSource()); treeObj.exportGrid = treeObj["export"]; treeObj.exportGrid("Treegrid/ExportToPdf", "", false); } function exportExcel() { var treeObj = $("#TreeGridContainer").ejTreeGrid("instance"); treeObj.model["exportData"] = JSON.stringify(treeObj.dataSource()); treeObj.exportGrid = treeObj["export"]; treeObj.exportGrid("Treegrid/ExportToExcel", "", false); } </script>
C#
public void ExportToPdf(string TreeGridModel) { PdfExport exp = new PdfExport(); TreeGridProperties obj = ConvertTreeGridObject(TreeGridModel); exp.Export(obj, exportData, new TreeGridExportSettings() { Theme = ExportTheme.FlatSaffron }, "Export"); } public void ExportToExcel(string TreeGridModel) { ExcelExport exp = new ExcelExport(); TreeGridProperties obj = ConvertTreeGridObject(TreeGridModel); exp.Export(obj, exportData, "Export.xlsx", ExcelVersion.Excel2010, new TreeGridExportSettings() { Theme = ExportTheme.FlatSaffron }); } private TreeGridProperties ConvertTreeGridObject(string gridProperty) { JavaScriptSerializer serializer = new JavaScriptSerializer(); IEnumerable div = (IEnumerable)serializer.Deserialize(gridProperty, typeof(IEnumerable)); TreeGridProperties gridProp = new TreeGridProperties(); foreach (KeyValuePair<string, object> ds in div) { var property = gridProp.GetType().GetProperty(ds.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); if (ds.Key == "exportData") { string str = Convert.ToString(ds.Value); exportData = JsonConvert.DeserializeObject<IEnumerable<BusinessObject>>(str); continue; } 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; }
A sample to export TreeGrid with client-side data is available here