如何使用 angularjs 将数据从 HTML table 导出到 excel

How to export data from HTML table to excel using angularjs

我想在单击按钮时使用 angularjs 将 html table 中的数据导出到 excel sheet。我尝试了一个代码,但在 vain.i 中虽然触发了按钮点击事件,但似乎没有其他事情发生

<table class="table table-bordered table-condensed table-hover  table-striped" id="tableId">
<tr ng-repeat="mas in vm1 | orderBy:orderByField:reverseSort">
                            <td>{{::mas.contractNumber}} </td>
                            <td>{{::mas.planNumber}} </td>
                            <td>{{::mas.businessErrorMsg }} </td>
                            <td>{{::mas.systemErrorMsg}} </td>

                        </tr>
 <button class="btn btn-link" ng-click="exportToExcel('#tableId')">
                            <span class="glyphicon glyphicon-share"></span>Export to Excel
                        </button>

//控制器代码

app.controller("ErrorDetailController", [
    "$scope", "$location", "$routeParams", "messageService", "errorService", "repositoryService", , "sharedPageService",
    function ($scope, $location, $routeParams, messageService, errorService, repositoryService,sharedPageService, **Excel, $timeout**) {
$scope.exportToExcel = function (tableId) { // ex: '#my-table'

            debugger;
            var exportHref = Excel.tableToExcel(tableId, 'sheet name');
            $timeout(function () { location.href = exportHref; }, 100); // trigger download
        }
}
]);

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

您可以使用 ng-table-to-csv 模块将 HTML 表格导出为 CSV 文件(可以在 Excel 中打开)。

正如该 repo 的自述文件中所给出的,这里是用法:

Getting Started / Usage

Install module via bower (or download the files from the dist folder in the repo):

shell bower install ng-table-to-csv --save

Add a reference to dist/ng-table-to-csv.js into your HTML pages.

Add ngTableToCsv as a dependency to your module:

js angular.module('your_app', ['ngTableToCsv']);

Add export-csv attribute directive on the table to define a new csv object on the scope with generate() and link() functions on them.

Options: - Use the separator attribute to change the default comma separator into something else (like semicolon). - Use the export-csv-ignore attribute to set the selector that will be used for prevent tr/th/td to be stringified.

To create an Export button from an anchro tag, use the generate() and link() functions mentioned above from ng-click and ng-href attributes of an anchor tag.

See below:

html <a class="btn" title="Export Table" ng-click='csv.generate()' ng-href="{{ csv.link() }}" download="myTable.csv"> <i class="glyphicon glyphicon-new-window"></i> &#160;Export </a> <table class="table table-bordered" export-csv="csv" separator=";"> <!-- table contents --> </table>

使用:

<body>{table}</body>

而不是:

<body><table>{table}</table></body> 在模板变量中。