How to export data from HTML table to excel using angularjs

I want to export data to html table in excel sheet using angularjs on abutton click. I tried the code but in vain.im it gets the button click event but nothing else happens

<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>

      

// controller code

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

      

+3


source to share


2 answers


You can use the module ng-table-to-csv

to export HTML tables to CSV files (which can be opened in Excel).

As stated in the README of this repo, here is the usage:



Getting started / using

Install the module via bower (or download files from folder dist

to repo):

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

Add a link to dist/ng-table-to-csv.js

your HTML pages.

Add ngTableToCsv

depending on your module:

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

Add attribute directive export-csv

on table

to define a new csv

object in scope with generate()

and link()

on functions .

Options: - Use an attribute separator

to change the default comma separator to something else (like a semicolon). - Use attribute export-csv-ignore

to set the selector to be used to prevent tr

/ th

/ td

.

To create a button Export

from an anchor label, use generate()

both the link()

from ng-click

and ng-href

anchor label attributes mentioned above .

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>

0


source


Using:

<body>{table}</body>

      



instead:

<body><table>{table}</table></body>

in a template variable.

+1


source







All Articles