js实现一键导出Excel

想要如下页面,一键导出Excel:

js实现一键导出Excel_第1张图片

html:

<div class="container">
        <table id="backViewTable" class="table table-hover table-sm table2excel">
            <tr>
                <td>#td>
                <td>IDtd>
                <td>姓名td>
                <td>座位号td>
                <td>操作td>
            tr>
            <tr>
                <th scope="row">1th>
                <td>1234td>
                <td>李文斐td>
                <td>2楼 3排 34号 td>
                <td><input class="btn-primary" type="button" value="删除"/>td>
            tr>
            <tr>
                <th scope="row">2th>
                <td>2345td>
                <td>lwftd>
                <td>1楼 3排 34号td>
                <td><input class="btn-primary" type="button" value="删除"/>td>
            tr>
            <tr>
                <th scope="row">3th>
                <td>3456td>
                <td>Leetd>
                <td>1楼 3排 12号td>
                <td><input class="btn-primary" type="button" value="删除"/>td>
            tr>
        table>
        <button class="btn btn-primary btn-sm" onclick="tablesToExcel(['backViewTable'], ['ProductDay1'], 'TestBook.xls', 'Excel')">Export to Excelbutton>
    div>

js:

<script>
    var tablesToExcel = (function() {
        var uri = 'data:application/vnd.ms-excel;base64,'
            , tmplWorkbookXML = ''
            + 'Axel Richter{created}'
            + ''
            + ''
            + ''
            + ''
            + '{worksheets}'
            , tmplWorksheetXML = '{rows}
'
, tmplCellXML = '{data}' , 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 function(tables, wsnames, wbname, appname) { var ctx = ""; var workbookXML = ""; var worksheetsXML = ""; var rowsXML = ""; for (var i = 0; i < tables.length; i++) { if (!tables[i].nodeType) tables[i] = document.getElementById(tables[i]); // 控制要导出的行数 for (var j = 0; j < tables[i].rows.length; j++) { rowsXML += ''; // 控制导出的列数(在本例中,最后一列为button,导出的文件会出错,所以导出到倒数第二列 for (var k = 0; k < tables[i].rows[j].cells.length-1; k++) { var dataType = tables[i].rows[j].cells[k].getAttribute("data-type"); var dataStyle = tables[i].rows[j].cells[k].getAttribute("data-style"); var dataValue = tables[i].rows[j].cells[k].getAttribute("data-value"); dataValue = (dataValue)?dataValue:tables[i].rows[j].cells[k].innerHTML; var dataFormula = tables[i].rows[j].cells[k].getAttribute("data-formula"); dataFormula = (dataFormula)?dataFormula:(appname=='Calc' && dataType=='DateTime')?dataValue:null; ctx = { attributeStyleID: (dataStyle=='Currency' || dataStyle=='Date')?' ss:StyleID="'+dataStyle+'"':'' , nameType: (dataType=='Number' || dataType=='DateTime' || dataType=='Boolean' || dataType=='Error')?dataType:'String' , data: (dataFormula)?'':dataValue , attributeFormula: (dataFormula)?' ss:Formula="'+dataFormula+'"':'' }; rowsXML += format(tmplCellXML, ctx); } rowsXML += '' } ctx = {rows: rowsXML, nameWS: wsnames[i] || 'Sheet' + i}; worksheetsXML += format(tmplWorksheetXML, ctx); rowsXML = ""; } ctx = {created: (new Date()).getTime(), worksheets: worksheetsXML}; workbookXML = format(tmplWorkbookXML, ctx); // 查看后台的打印输出 console.log(workbookXML); var link = document.createElement("A"); link.href = uri + base64(workbookXML); link.download = wbname || 'Workbook.xls'; link.target = '_blank'; document.body.appendChild(link); link.click(); document.body.removeChild(link); } })();
script>

点击“export to Excel”,便开始下载导出的Excel,,打开如下:
js实现一键导出Excel_第2张图片

参考链接:Here is Full Example

你可能感兴趣的:(JavaScript)