1

I am trying to export a HTML table into an excel sheet and I am using alasql:

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

This doesn't work for me because my data looks like:

$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}},
]

When I use the export I get [object,object] as a result. I know the reason is that because my array is of objects of objects. How do I get the some[] to show up in the excel with the name as heading?

In Excel, I want the data to look like

    Faculty   |   Student
    ----------------------
    MIT
    ----------------------
    20k        | 500k
agershun
  • 4,077
  • 38
  • 41
user3100209
  • 357
  • 1
  • 4
  • 17
  • What library do you use for export data to csv or excel? Also, could you give an example of `some[]`? Export the data like this is not a problem, it just depends on how you want the data look like in the csv file. So, in your case, you need to do some data modification and then export as a excel or csv file. – Shaohao Dec 09 '15 at 14:12
  • @ShaohaoLin I am using the alasql library ... i edited the question look at the data again. – user3100209 Dec 09 '15 at 17:10
  • I am able to do `Name | Faculty | Student ` to`MIT | 20K | 50K`. If you want to learn how to do that, let me know, I will post the solution. – Shaohao Dec 09 '15 at 17:23
  • @ShaohaoLin That would work for me thanks – user3100209 Dec 09 '15 at 17:45
  • The data you posted is not a valid `json` data. Can you make it valid to an json data? Or you can't? – Shaohao Dec 09 '15 at 18:27
  • @ShaohaoLin I'm sorry. I think i formatted it incorrectly. Here is the link to the data http://www.jsoneditoronline.org/?id=99e0bb584025720b8d7dc0a10f49cb44 – user3100209 Dec 09 '15 at 18:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97439/discussion-between-shaohao-lin-and-user3100209). – Shaohao Dec 09 '15 at 18:39
  • The link works. The data fields the you want to export are `Name`, `Faculty` and `Student `? – Shaohao Dec 09 '15 at 18:41
  • @ShaohaoLin I'm in the chat room, we can talk there – user3100209 Dec 09 '15 at 19:22

2 Answers2

1

Assuming the data is the link you provided in the comment session. The following function performances converting the [object object] to an array of object, which allow to export as CSV file.

//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);
  });
});

After the function, you $scope.exportCsvFile array will look like:

[
 {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"},
 {...}
]

After this, you can export the file:

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

The Csv File should be like as following:

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

If this still does not solve your problem, you can take a look of an AngularJS directive called ng-Csv. It performances export data as csv file in the font-end side. Hope it helps!!

Shaohao
  • 3,471
  • 7
  • 26
  • 45
0

There is a problem with JSON data in your question, but if you modify it to something like:

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

You can try this SQL statement:

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

Here you can use -> arrow as point in JavaScript:

[{data:[10]}]            -- SELECT data->0      gives 10
[{data:{other:20}}]      -- SELECT data->other  gives 20
Shaohao
  • 3,471
  • 7
  • 26
  • 45
agershun
  • 4,077
  • 38
  • 41