How to export my json data into pdf,excel using angular 2

前端 未结 4 901
慢半拍i
慢半拍i 2021-01-05 13:52

I have created my Data table from the angular 2 website. Now I want to export my json data to PDF,excel using angular 2 framework.

Kindly give me s

4条回答
  •  走了就别回头了
    2021-01-05 14:14

    (function () {
        'use strict';
    
        angular.module('ngJsonExportExcel', [])
            .directive('ngJsonExportExcel', function () {
    
                return {
                    restrict: 'AE',
                    scope: {
                        data : '=',
                        filename: '=?',
                        reportFields: '=',
                        separator: '@'
                    },
    
                    link: function (scope, element) {
                        scope.filename = !!scope.filename ? scope.filename : 'SalesReport';
    
                        var fields = [];
                        var header = [];
                        var separator = scope.separator || ',';
    
                        angular.forEach(scope.reportFields, function(field, key) {
                            if(!field || !key) {
                                throw new Error('error json report fields');
                            }
    
                            fields.push(key);
                            header.push(field);
                        });
    
                        element.bind('click', function() {
                            var bodyData = _bodyData();
                            var strData = _convertToExcel(bodyData);
    
                            var blob = new Blob([strData], {type: "text/plain;charset=utf-8"});
    
                            return saveAs(blob, [scope.filename + '.csv']);
                        });
    
                        function _bodyData() {
                            var data = scope.data;
                            var body = "";
                            angular.forEach(data, function(dataItem) {
                                var rowItems = [];
    
                                angular.forEach(fields, function(field) {
                                    if(field.indexOf('.')) {
                                        field = field.split(".");
                                        var curItem = dataItem;
    
                                        // deep access to obect property
                                        angular.forEach(field, function(prop){
                                            if (curItem !== null && curItem !== undefined) {
                                                curItem = curItem[prop];
                                            }
                                        });
    
                                        data = curItem;
                                    }
                                    else {
                                        data = dataItem[field];
                                    }
    
                                    var fieldValue = data !== null ? data : ' ';
    
                                    if (fieldValue !== undefined && angular.isObject(fieldValue)) {
                                        fieldValue = _objectToString(fieldValue);
                                    }
    
                                    if(typeof fieldValue == 'string') {
                                        rowItems.push('"' + fieldValue.replace(/"/g, '""') + '"');
                                    } else {
                                        rowItems.push(fieldValue);
                                    }
                                });
    
                                body += rowItems.join(separator) + '\n';
                            });
    
                            return body;
                        }
    
                        function _convertToExcel(body) {
                            return header.join(separator) + '\n' + body;
                        }
    
                        function _objectToString(object) {
                            var output = '';
                            angular.forEach(object, function(value, key) {
                                output += key + ':' + value + ' ';
                            });
    
                            return '"' + output + '"';
                        }
                    }
                };
            });
    })();
    

提交回复
热议问题