Articles in this section
Category / Section

How to export the Grid in AngularJS?

4 mins read

We can export a grid in angular JS using “WebApiAdaptor in DataManager to retrieve data from WebApi Service as follows.

Grid Rendering Code:               

<div id="grid" ej-grid e-datasource="data" e-columns="col" e-toolbarclick="toolbarHandler" e-toolbarsettings-showtoolbar='true' e-toolbarsettings-toolbaritems='toolbar' e-allowpaging="true"></div>

 

JavaScript Code:

Here the toolbar contains ExcelExport, WordExport, PdfExport icons that are used to perform exporting. When we click the toolbar exporting icon, it internally invokes the export() public method of Grid object to make export.

And also mappers support is not applicable for JavaScript platform since its running in client side. But we can achieve this by using toolbarClick clientside event of the grid as in the below snippet.

angular.module('listCtrl', ['ejangular'])
.controller('PhoneListCtrl', function ($scope) {
$scope.col = [
                  { field: "OrderID", width: 80, headerText: "Order ID" },
                  { field: "CustomerID", width: 75, headerText: "Customer ID"},
                  { field: "EmployeeID", width: 75, headerText: "Employee ID"},
                  { field: "ShipCity", headerText: 'Ship City, width: 90 }
        ]
$scope.data = ej.DataManager({ url: "api/Values", adaptor: "WebApiAdaptor" });
        $scope.toolbar = ["excelExport", "pdfExport","wordExport"];
        $scope.toolbarHandler = function (args) {
            if (args.itemName == "Excel Export") {
                args.cancel = true;
                this.export("api/Values/ExcelExport");
//Same for pdf and word documents
            }

 

ValuesController.cs

Exporting is achieved by using ExcelExport action controller method. While using WebApi Service we need to return the data as PageResult as we need both data and its count for paging operation.

In controller method (ConvertGridObject), Grid property is passed as string parameter. We need to serialize it into Grid Property. Using Export() server method we can export the Grid into excel, pdf and word documents.

public class ValuesController : ApiController
    {
        // GET api/<controller>
        NORTHWNDEntities db = new NORTHWNDEntities();
        public PageResult<Order> Get(ODataQueryOptions opts)
        {
            var results = opts.ApplyTo(db.Orders.AsQueryable());
            var data = db.Orders.AsQueryable();
            return new PageResult<Order>((IEnumerable<Order>)results, Request.GetNextPageLink(), data.Count());
 
        }
 
// POST api/<controller>
        [System.Web.Http.ActionName("ExcelExport")]
        [AcceptVerbs("POST")]
        public void ExcelExport()
        {
            string gridModel = HttpContext.Current.Request.Params["GridModel"];
            GridProperties gridProperty = ConvertGridObject(gridModel);
            ExcelExport exp = new ExcelExport();
           
            IEnumerable<Order> result = db.Orders.ToList();
//Exporting grid using Export method by passing the grid obj, dataSource,type of document,version,hidecolumn include,templatecolumn and theme
            exp.Export(gridProperty, result, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
         }
//Same for pdf and word documents
 
  private GridProperties ConvertGridObject(string gridProperty)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
GridProperties gridProp = new GridProperties();
            foreach (KeyValuePair<string, object> ds in div)
            {
                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;
        

Result:

Figure: Exporting using Angular JS

 

 

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied