1

我正在尝试将 HTML 表导出到 excel 表中,并且正在使用 alasql:

$scope.Excelexport =function(){
  alasql('SELECT * INTO XLSX("example.xlsx",{headers:true}) FROM ?',[$scope.college.data]);
}

这对我不起作用,因为我的数据如下所示:

$scope.college.data =  [
  {{[{faculty: 20k, Student:5k}], name:MIT}}, 
  {{[{faculty: 20k, Student:5k}], name:Stevens}}, 
  {{[{faculty: 20k, Student:5k}], name:RIT}}, 
  {{[{faculty: 20k, Student:5k}], name:IIT}},
]

当我使用导出时,我得到[object,object]了结果。我知道原因是因为我的数组是对象的对象。如何some[]以名称作为标题显示在 Excel 中?

在 Excel 中,我希望数据看起来像

    Faculty   |   Student
    ----------------------
    MIT
    ----------------------
    20k        | 500k
4

2 回答 2

1

假设数据是您在评论会话中提供的链接。以下函数将 转换[object object]为对象数组,允许导出为 CSV 文件。

//initialize an empty array ready for export as csv
$scope.exportCsvFile = [];

//$scope.data is the valid json data you provided on the comment session
//you just assign the json data to $scope.data
angular.forEach($scope.data, function(collegeData){
  var labbox = collegeData.data.labbox;
  angular.forEach(collegeData.data.timeseriesdata, function(singleData){
    var temp = {
      name: labbox
    };
    angular.forEach(singleData, function(value, key){
        temp[key] = value;
    });
    $scope.exportCsvFile.push(temp);
  });
});

在函数之后,您的$scope.exportCsvFile数组将如下所示:

[
 {name: 'Stevens', faculty: 1, student, 949, from: "17:30", to: "17:42"},
 {name: 'Stevens', faculty: 0.99, student, 949, from: "17:42", to: "17:54"},
 {name: 'Stevens', faculty: 0.97, student, 1380, from: "17:54", to: "17:42"},
 {...}
]

在此之后,您可以导出文件:

$scope.Excelexport = function() {
 alasql('SELECT * INTO XLSX("example.xlsx",{headers:true}) FROM ?',[$scope.exportCsvFile]);
}

CSV 文件应如下所示:

    Name   |   Faculty   |   Student   |   From    |    To
    ---------------------------------------------------------
    Stevens|      1      |     949     |   17:30   |   17:42
    ---------------------------------------------------------
    Stevens|    0.99     |     1090    |   17:42   |   17:54
    ---------------------------------------------------------

如果这仍然不能解决您的问题,您可以查看一个名为ng-Csv的 AngularJS 指令。它在字体端将数据导出为 csv 文件。希望能帮助到你!!

于 2015-12-09T19:11:12.593 回答
0

您的问题中的 JSON 数据存在问题,但如果您将其修改为:

$scope.college.data = [
  {data:[{faculty: '20k', Student:'5k'}], name: 'MIT' }}, 
  {data:[{faculty: '20k', Student:'5k'}], name: 'Stevens' }}, 
];

你可以试试这个 SQL 语句:

alasql('SELECT data->0->faculty,data->0->faculty,\
  name INTO XLSX("example.xlsx",{headers:true}) FROM ?',[$scope.college.data])

在这里,您可以->在 JavaScript 中使用箭头作为点:

[{data:[10]}]            -- SELECT data->0      gives 10
[{data:{other:20}}]      -- SELECT data->other  gives 20
于 2015-12-10T07:52:46.253 回答