Angular JS - How to export Javascript Object to XLS file ?

前端 未结 2 1422
遥遥无期
遥遥无期 2021-01-12 15:14

Actually, I\'ve an object in my controller, i just want to export that object as .xls or .csv file.i used a lot of approaches like this:

HTML

<
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 15:58

    Yes, you can save you data with Alasql JavaScript library with XLSX.js library. Here is an example:

    First: include two JavaScript libraries into your page:

    • alasql.min.js
    • xlsx.core.min.js

    Second: replace exportData() function in your code with:

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

    Third: for CSV files - simply use CSV() function:

      alasql('SELECT * INTO CSV("john.csv",{headers:true, separator:";"}) FROM ?',[$scope.items]);
    

    You can play with this example in jsFiddle.

提交回复
热议问题