javascript - 使用 ng-csv 将 HTML 页面中的表格导出到 Excel

标签 javascript angularjs excel csv yeoman

我使用 Yeoman Angular 生成器创建了一个简单的项目。我用这个 API 做了一张 table .

这是我的 main.html:

<div ng-controller="MainCtrl">
<button type="button" ng-csv="getArray" csv-header="getHeader()" filename="test.csv">Export</button>
</div>

<div class="row">
        <div class="col-xs-12 table">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>No.</th>
                        <th>ID Pel.</th>
                        <th>Cust ID</th>
                        <th>Meter SN</th>
                        <th>Readingpoint</th>
                        <th>Last Dial</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="pop in tableData">
                        <td>{{pop.No}}</td>
                        <td>{{pop.idPel}}</td>
                        <td>{{pop.custID}}</td>
                        <td>{{pop.uSN}}</td>
                        <td><a ui-sref="app.bill_detail3">{{pop.metername}}</a></td>
                        <td>17-02-2015 | 16.00</td>
                        <td><span class="label label-success">Online</span></td>
                    </tr>

                </tbody>
            </table>
        </div>
        <!-- /.col -->
    </div>

这是我的服务/main.js

angular.module('simpleApp')
  .service('main', function ($http, $q, ApiEndpoint) {
    // AngularJS will instantiate a singleton by calling "new" on this function
  var service = {};

        service.getPopList = getPopList;

        function getPopList(){
            var q = $q.defer();

            var req = {
                method: 'GET',
                url: ApiEndpoint.url + 'getpop/list'
            }

            $http(req)
                .success(function(data) {
                    q.resolve(data);
                })
                .error(function(error){
                    q.reject(error);
                })

            return q.promise;
        };

        return service;
  });

这是我的 Controller /main.js:

angular.module('simpleApp')
  .controller('MainCtrl', function ($scope, main) {
    this.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];

    $scope.getHeader = function () {return ["No.", "ID Pel.", "Cust ID", "Meter SN", "Readingpoint", "Last Dial", "Status"]};


    $scope.tableData = [];
    $scope.meterCount = 0;

    main.getPopList().then(function(data){
        var no = 0;
        for (var i = 0; i < data.length; i++) {
            for (var j = 0; j < data[i].d.readpoint.length; j++) {
                $scope.tableData.push({
                    'No': ++no,
                    '_id': data[i]._id,
                    'siteID': data[i].d.siteID,
                    'uSN': data[i].d.readpoint[j].uSN,
                    'idPel': data[i].d.readpoint[j].idPel,
                    'custID': data[i].d.readpoint[j].custID,
                    'metername': data[i].d.readpoint[j].metername
                });
            }
        }

      $scope.getArray = [{a:'No', b:'_id', c:'siteID', d:'uSN', e:'idPel', f:'custID', g:'metername'},
      {a:'No', b:'_id', c:'siteID', d:'uSN', e:'idPel', f:'custID', g:'metername'}];


        $scope.meterCount = no;
    }, function(){ });

  });

网络看起来像这样: enter image description here

我想将所有表格内容(标题和内容本身)导出到 Excel (.xls)。

我该怎么做?

哦,我现在正在使用 ng-csv。现在的结果是这样的: enter image description here

最佳答案

将按钮的 ng-csv 属性值更改为 tableData

<button type="button" ng-csv="tableData" csv-header="getHeader()" filename="test.csv">Export</button>

关于javascript - 使用 ng-csv 将 HTML 页面中的表格导出到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32585689/

相关文章:

javascript - 在 Node.js 中不使用 readFileSync() 串行读取多个文件

javascript - 如何在 mustache 中呈现数组的一个元素

javascript - 即使平移时,OrbitControls 仍然在模型原点上旋转吗?

javascript - 为复选框列表项列表设置焦点

javascript - 在数组 typescript 和 angularjs 和 ionic 中查找值

javascript - 为什么使用 javascript 编辑输入文本不更新 angularJs 变量?

vba - # 在超链接中防止复制完整的超链接

vba - 当存储在集合中时,如何更改类属性的值

sql - 我正在尝试在 Access VBA 中更新表中的某些字段

routes - 通过 angularjs 中的路由进行重定向