11

I am trying to export data to excel in angular js

1) User clicks a button 2) Data in $scope.myArray gets saved to excel file.

I tried

var blob = new Blob($scope.myArray , {
    type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
});
saveAs(blob, "Report.xls");
};

It prompts to open the excel file. But whenever I try to open it, it says the file format or file extension is not valid.

Any Help!

Ash
  • 2,108
  • 2
  • 17
  • 22
Amit Kumar
  • 591
  • 2
  • 8
  • 24

2 Answers2

11

Try the following code that will help you to create an excel file for you.

var result = ["Item 1", "Item 3"];
const myJsonString = JSON.stringify(result);
const blob = new Blob([myJsonString], {
  type: "application/vnd.ms-excel;charset=utf-8"
});
saveAs(blob, "Report.xls");

Demo

Shohel
  • 3,886
  • 4
  • 38
  • 75
  • $scope.myArray must be string type, wait – Shohel Nov 03 '15 at 09:58
  • It Worked, one more ques. It shows lots of [object Object] to me, myArray contains following data .....[Object { IdeaNumber=31, Description="amit1", Department="RBVH/ETI1", more...}, Object { IdeaNumber=32, Description="Testing nutshell", Department="RBEI/BSW2", more...}, Object { IdeaNumber=35, Description="Testing nutshell", Department="RBEI/BSW2", more...}] – Amit Kumar Nov 03 '15 at 10:02
  • One more! now it shows in one row ..any help to show in tabluar?? [{"IdeaNumber":31,"Description":"amit1","Department":"RBVH/ETI1","Status":"Implemented","StatusColor":"idea-status-implemented","$$hashKey":"object:11"},{"IdeaNumber":32,"Description":"Testing nutshell","Department":"RBEI/BSW2","Status":"Implemented","StatusColor":"idea-status-implemented","$$hashKey":"object:12"},{"IdeaNumber":35,"Description":"Testing nutshell","Department":"RBEI/BSW2","Status":"Waiting for implementation","StatusColor":"idea-status-waiting-for-implementation","$$hashKey":"object:13"}, – Amit Kumar Nov 03 '15 at 10:24
  • I do not know brother – Shohel Nov 03 '15 at 10:33
  • @Amit Kumar have you completed multiline export of data ? – kushalbhaktajoshi May 05 '16 at 06:53
  • My data is like ['1','2','3']. The final result will be only one cell with data ["1","2","3"]. Can anybody give an example to show what's inside myArray? Thanks. – Bagusflyer Feb 02 '17 at 15:09
  • The way I fixed the inline JSON issue was to dynamically convert it to a html table using jquery.The SaveAs does work pretty good with html structures, but I was not able to make it work with JSON – Juan Zamora Mar 20 '17 at 21:43
  • @Shohel regarding my question here (https://stackoverflow.com/questions/43943003/encoding-csv-clrf-into-newlines), how can I ensure CLRF encoding is working on csv files (whether excel or google sheets). It's not displaying the spaces / multilines I'm expecting when I open the CSV – user3871 May 12 '17 at 21:03
  • Please add a more comprehensive answer which everyone reading can benefit from. Please declare what's `result`. – letsc Mar 13 '19 at 13:30
1
 csv = ['name','md5','desc']
  _.map $scope.trustFileList, (_file) ->
    csv.push "\n#{_file.name}"
    csv.push _file.md5
    csv.push _file.desc

  blob = new Blob([csv], {
    type: "application/json;charset=utf-8"
  });

  saveAs(blob, "TrustFileWhiteList.xls");