如何将嵌套的json数组数据解析(转换)为angularjs中的csv格式以导出文件xlsx格式

时间:2017-02-16 13:49:05

标签: angularjs

  

您需要导出文件xlsx格式,但我确实有嵌套数据   json数组格式由于这个我无法获取数据,可以任何一个   帮助我,因为我试图但不能正确获取数据

    <html ng-app="myApp">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="alasql.min.js"></script>
    <script src="xlsx.core.min.js"></script>

    <script src="https://rawgithub.com/eligrey/FileSaver.js/master/FileSaver.js" type="text/javascript"></script>
<button ng-click="exportData()">Export</button>
    <div ng-controller="myCtrl">
    <html>

我的脚本是

.fucntion($scope){
 $scope.inverters = [
  {
    "InvDetails": "UPS",
    "LstRecords": [
      {
        "Id": 1,
        "Invertor_Id": 1,
        "Time_of_Reading": "20170214",
        "Lastreading": 0,
        "Readingby": 0
      },
      {
        "Id": 87,
        "Invertor_Id": 1,
        "Time_of_Reading": "20170215",
        "Lastreading": 5,
        "Readingby": 10
      },
      {
        "Id": 110,
        "Invertor_Id": 1,
        "Time_of_Reading": "20170216",
        "Lastreading": 10,
        "Readingby": 92
      },
      {
        "Id": 111,
        "Invertor_Id": 1,
        "Time_of_Reading": "20170216",
        "Lastreading": 92,
        "Readingby": 95
      }
    ]
  },
  {
    "InvDetails": "Power Supply",

    "LstRecords": [
      {
        "Id": 2,
        "Invertor_Id": 2,
        "Time_of_Reading": "20170214",
        "Lastreading": 0,
        "Readingby": 0
      },
      {
        "Id": 88,
        "Invertor_Id": 2,
        "Time_of_Reading": "20170215",
        "Lastreading": 7,
        "Readingby": 13
      },
      {
        "Id": 109,
        "Invertor_Id": 2,
        "Time_of_Reading": "20170216",
        "Lastreading": 13,
        "Readingby": 25
      },
      {
        "Id": 112,
        "Invertor_Id": 2,
        "Time_of_Reading": "20170216",
        "Lastreading": 25,
        "Readingby": 49
      }
    ]
  }
];
$scope.exportData = function () {
        var data="";
        $scope.headers=[];
         angular.forEach($scope.inverters,function(value,key){

        var we=value.InvDetails;
        $scope.headers.push(we);
        $scope.last=value.LstRecords;
        angular.forEach($scope.last,function(value,key){

            data={
                "Id": value.Id,
                "Invertor_Id": value.Invertor_Id,
                "Time_of_Reading":value.Time_of_Reading,
                "Lastreading": value.Lastreading,
                 "Readingby": value.Readingby
            };
        })
    })

         $scope.result=[];
         $scope.result0=[];

         $scope.result.push({
            "Invertor1":JSON.stringify(data)


         })

        alasql('SELECT * INTO XLSX("john.xlsx",{headers:true}) FROM ?',[result]);
    };
};
  

任何人都可以帮我解决如何导出这个,如果我需要拆分   数据,因为我从API获取数据,所以如果你拆分并导出它   将有用的形式是任何其他解决方案是有意味着让我   知道该怎么做。提前谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用SEARCH运算符:

alasql('SEARCH / AS @r     \
        LstRecords / AS @l \
        RETURN(@r->InvDetails AS InvDetails,  \
               @l->Id AS Id,                  \
               @l->Invertor_Id AS Inventor_Id \
        ) \
        INTO XLSX("john.xlsx",{headers:true}) FROM ?',[result]);

这里AlaSQL遍历第一个数组并将当前记录保存到@r临时变化。然后它遍历嵌套的LstRecords数组并将其保存到@l变量。 RETURN子句生成包含所有必填字段的结果对象。