jQuery 使用 FileSaver.js 保存 excel 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33495979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:57:33  来源:igfitidea点击:

save excel file using FileSaver.js

jqueryangularjsexport-to-excelfilesaver.js

提问by Amit Kumar

I am trying to export data to excel in angular js

我正在尝试将数据导出到 Angular js 中的 excel

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

1) 用户单击按钮 2) $scope.myArray 中的数据被保存到 excel 文件中。

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.

它提示打开excel文件。但是每当我尝试打开它时,它都会说文件格式或文件扩展名无效。

Any Help!

任何帮助!

回答by Shohel

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

尝试以下代码,它将帮助您为您创建一个 excel 文件。

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

演示

回答by user7129978

 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");