Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save excel file using FileSaver.js

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!

like image 484
Amit Kumar Avatar asked Nov 03 '15 09:11

Amit Kumar


People also ask

How do I save a .xlsx file as a blob?

var blob = new Blob( [data], {type: "application/vnd. openxmlformats-officedocument. spreadsheetml. sheet;base64,"} ); // Programatically create a link and click it: var a = document.


2 Answers

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

like image 104
Shohel Avatar answered Sep 20 '22 05:09

Shohel


 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");
like image 21
user7129978 Avatar answered Sep 20 '22 05:09

user7129978