Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular JS-如何将Javascript对象导出到XLS文件?_Javascript_Angularjs_Csv_Export_Xls - Fatal编程技术网

Angular JS-如何将Javascript对象导出到XLS文件?

Angular JS-如何将Javascript对象导出到XLS文件?,javascript,angularjs,csv,export,xls,Javascript,Angularjs,Csv,Export,Xls,实际上,我的控制器中有一个对象,我只想将该对象导出为.xls或.csv文件。我使用了很多类似的方法: HTML <script src="https://rawgithub.com/eligrey/FileSaver.js/master/FileSaver.js" type="text/javascript" /> <div ng-controller="myCtrl"> <button ng-click="exportData()">Export&l

实际上,我的控制器中有一个对象,我只想将该对象导出为.xls或.csv文件。我使用了很多类似的方法:

HTML

<script src="https://rawgithub.com/eligrey/FileSaver.js/master/FileSaver.js" type="text/javascript" />
<div ng-controller="myCtrl">
    <button ng-click="exportData()">Export</button>
    <br />
    <div id="exportable">
    <table width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>DoB</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="item in items">
                <td>{{item.name}}</td>
                <td>{{item.email}}</td>
                <td>{{item.dob | date:'MM/dd/yy'}}</td>
            </tr>
        </tbody>
    </table>
    </div>
</div>

但这不适用于分页表。是否有任何方法可以直接将对象(在本例中为$scope.item)导出到文件(xls、csv)中

如果您对CSV文件感到满意,那么该模块就是最佳选择。您不从DOM加载元素,而是直接导出数组。在这里,您可以看到一个正在运行的示例

html:

 <h2>Export {{sample}}</h2>
  <div>
      <button type="button" ng-csv="getArray" filename="test.csv">Export</button>
</div>

是的,您可以使用带有XLSX.js库的JavaScript库保存数据。以下是一个例子:

第一:在页面中包含两个JavaScript库:

  • alasql.min.js
  • xlsx.core.min.js
第二:将代码中的exportData()函数替换为:

  $scope.exportData = function () {
      alasql('SELECT * INTO XLSX("john.xlsx",{headers:true}) FROM ?',[$scope.items]);
  };
第三:对于CSV文件-只需使用CSV()函数:

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

你可以玩。

当然可以。我使用了这个模块。但我的目标是将过滤后的对象直接导出到
Excel
文件中。好吧,您在问题中指出需要导出到.xls或.csv文件。您可以始终对数组应用筛选器(如果.csv仍然有效)。这是一个很好的解决方案,非常感谢您的贡献。您是否有任何简单的解决方案来解析任何内部对象和集合?是的。您可以尝试使用搜索运算符,该运算符类似于SELECT,但它在JSON对象内部进行遍历(请参见此处:)当您尝试打开plunkr xlsx文件时,该文件出现问题。对于查找xlsx.core dist的任何人:
  $scope.exportData = function () {
      alasql('SELECT * INTO XLSX("john.xlsx",{headers:true}) FROM ?',[$scope.items]);
  };
  alasql('SELECT * INTO CSV("john.csv",{headers:true, separator:";"}) FROM ?',[$scope.items]);