Export HTML table to Excel file

前端 未结 6 1940
我在风中等你
我在风中等你 2021-01-14 15:14

I\'ve been looking into some jQuery plugins that are capable of doing this. I decided to use the one at http://www.jqueryscript.net/table/Export-Html-Table-To-Excel-Spreads

6条回答
  •  情歌与酒
    2021-01-14 15:32

    As @charlietfl said you need the .click() binding inside the $(document).ready because if not the element doesn't exist when you try to bind it.

    And this way you dont need to use a plugin

    function exportGrid(gridID,filename) {
        var html = $('#' + gridID).html();
        var a = document.createElement('a');
        a.id = 'tempLink';
        a.href = 'data:application/vnd.ms-excel,' + html;
        a.download = filename + ".xls";
        document.body.appendChild(a);
        a.click(); // Downloads the excel document
        document.getElementById('tempLink').remove();
    }
    $(document).ready(function() {
        $("button").click(function(){
            exportGrid("TheGridId","The excel name");
        });
    });
    

    Here you have a example https://jsfiddle.net/0mzn7uLq/

提交回复
热议问题