Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
192
rated 0 times [  195] [ 3]  / answers: 1 / hits: 26422  / 8 Years ago, tue, september 13, 2016, 12:00:00

I'm extracting an array with 4 objects and each object has an array inside, from my kendo charts datasource, on my Angular project.



The data inside each sub-object varies in size, but it always includes a timestamp, and 1-5 value fields.



I need to export this array to an Excel file (.xls or .xlsx NOT CSV).



So far I managed to download the JSON as a file on its own (both .json and unformatted .xls).



I'd like for each object to be a book and in that book to have a formatting that has the timestamp in the first column, value 1 in another, and so on. The header for the columns should be timestamp, value1 name, etc (I'm translating these on the ui according to user preferences).



How can I build this type of formatted .xls file using angular? I don't know a particular good library for this, that is clear on how to use it in Angular.


More From » angularjs

 Answers
58

Following Nathan Beck's link sugestion, I used AlaSQL. I'm getting correctly formatted columns, just need to adapt my array to have multiple worksheets.



The way we integrate alaSQL into our Angular project is by including the alasql.min.js and xlsx.core.min.js.



Then we call the alasql method in our function



$scope.export = function(){
var arrayToExport = [{id:1, name:gas},...];
alasql('SELECT * INTO XLSX(your_filename.xlsx,{headers:true}) FROM ?', arrayToExport);
}


EDIT: Solved the multiple worksheets issues as well. Keep in mind that when using the multiple worksheet method, you have to remove the asterisk and replace the headers: true object in the query with a question mark, passing the options in a separate array. So:



var arrayToExport1 = [{id:1, name:gas},...];
var arrayToExport2 = [{id:1, name:solid},...];
var arrayToExport3 = [{id:1, name:liquid},...];
var finalArray = arrayToExport1.concat(arrayToExport2, arrayToExport3);

var opts = [{sheetid: gas, headers: true},{sheetid: solid, headers: true},{sheetid: liquid, headers: true}];
alasql('SELECT INTO XLSX(your_filename.xlsx,?) FROM ?', [opts, finalArray]);

[#60726] Sunday, September 11, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yosefleod

Total Points: 113
Total Questions: 100
Total Answers: 115

Location: Egypt
Member since Tue, May 3, 2022
2 Years ago
;