繁体   English   中英

将HTML表格导出到Excel

[英]Export an HTML table to Excel

我已经创建了一个应用程序,该应用程序基于JSON数据集填充表,并且我想将过滤的数据存储到Excel文件(甚至CSV)中。 我有两个脚本文件,app.js和mainController.js(分开以获得更清晰的代码),一个视图和索引文件(当然还有引导程序)。 这是app.js:

(function () {
  "use strict";

  var app = angular.module('customAudience', ['ngRoute']);

  app.config(function ($routeProvider) {

    $routeProvider
      .when('/', {
        controller: 'mainController',
        templateUrl: 'views/mainPage.html'
      })
      .otherwise({
        redirectTo: '/'
      });
  });

  app.factory('Excel', function ($window) {
  var uri = 'data:application/vnd.ms-excel;base64,',
template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:x="urn:schemas-microsoft-com:office:excel"xmlns="http://www.w3.org/TR/REC-
html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>
<x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions>
<x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets>
</x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body>
</html>',
    base64 = function (s) {
      return $window.btoa(unescape(encodeURIComponent(s)));
    },
    format = function (s, c) {
      return s.replace(/{(\w+)}/g, function (m, p) {
        return c[p];
      })
    };
  return {
    tableToExcel: function (tableId, worksheetName) {
      var table = $(tableId),
        ctx = {
          worksheet: worksheetName,
          table: table.html()
        },
        href = uri + base64(format(template, ctx));
      return href;
    }
  };
});
}());

这是我的mainController.js:

(function () {

  var mainController = function ($scope, $http, Excel, $timeout) {

    $scope.sortType = 'PhoneNumber'; //set the default sort type
    $scope.sortReverse = false; //set the default sort order
    $scope.searchElement = ''; //set the default search/filter term

    $scope.items = [];
    $http.get('data/data.json').then(function (response) {
      $scope.items = response.data;
    });

    $scope.exportToExcel = function (tableId) { // ex: '#my-table'
      $scope.exportHref = Excel.tableToExcel(tableId, 'sheet name');
      $timeout(function () {
        location.href = $scope.fileData.exportHref;
      }, 100); // trigger download
    };
  };


  angular.module('customAudience').controller('mainController', 
['$scope', '$http', 'Excel', '$timeout', mainController]);

}());

我的桌子上有一个ID,上面写着“ mytable”,并将其传递给函数。

我得到的错误是:

TypeError:无法读取e。(angular.js:5507)在angular.js:5784处angular.js:17855在mainController.js:19处未定义的属性'exportHref'*

我已经使用https://gist.github.com/umidjons/352da2a4209691d425d4作为指南,但这是行不通的。

你们中有没有人在AngularJS中将HTML表导出到Excel?

我想念什么吗?

请注意,我使用的是Mac,这有什么区别吗?

是否有另一种更干净的导出数据的方法-像CSV文件一样?

尝试通过$ scope.exportHref在$ scope.exportToExcel方法中更改location.href值; 没有fileData。

$timeout(function () { location.href = $scope.exportHref; }, 100); // trigger download

这对我行得通。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM