How to export data from HTML table to excel using angularjs

15,304

Solution 1

You can use the ng-table-to-csv module to export HTML tables into CSV files (that can be opened in Excel).

As given on the README of that repo, here is the usage:

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>

Solution 2

Use :

<body>{table}</body>

instead of :

<body><table>{table}</table></body> in template variable.

Share:
15,304
pankaj
Author by

pankaj

Updated on June 05, 2022

Comments

  • pankaj
    pankaj almost 2 years

    I want to export data in my html table to an excel sheet using angularjs on abutton click. I tried a code, but in vain.i m getting the button click event triggered though but nothing else seems to happen

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