美文网首页
xlsx浏览器导出

xlsx浏览器导出

作者: antlove | 来源:发表于2021-05-18 09:14 被阅读0次

    github地址

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>导出XLSX</title>
    
        <script src="./xlsx.full.min.js"></script>
    
        <style>
            table {border-collapse: collapse;margin-bottom: 20px;}
            table td, table th {border: 2px solid #ccc; }
        </style>
    </head>
    <body>
    
        <table id="data-table">
            <thead>
                <tr>
                    <th>id</th>
                    <th>name</th>
                </tr>
    
            </thead>
    
            <tbody>
                <tr>
                    <td>001</td>
                    <td>王大锤</td>
                </tr>
                <tr>
                    <td>002</td>
                    <td>马大炮</td>
                </tr>
            </tbody>
        </table>
    
        <button onclick="exportFromTable()">从Table导出</button>
    
        <button onclick="exportFromJSON()">从JSON导出</button>
    
        <script>
            
            function exportFromTable() {
                var elt = document.getElementById('data-table');
                var sheetName = "table-sheet";
                var wb = XLSX.utils.table_to_book(elt, { sheet: sheetName});
                var fileName = "export-from-table.xlsx";
                return XLSX.writeFile(wb, fileName);
            }
    
            function exportFromJSON() {
                // 创建一个空的workbook
                var wb = XLSX.utils.book_new();
    
                var header = ['id', 'name'];
                var data = [
                    {id: '001', name: '王大锤' },
                    {id: '002', name: '马大炮' }
                ];
                // 输出是否忽略标头
                var skipHeader = false;
                var ws = XLSX.utils.json_to_sheet(data, {header: header, skipHeader: skipHeader});
                var sheetName = 'json-sheet';
                var fileName = "export-from-json.xlsx";
                XLSX.utils.book_append_sheet(wb, ws, sheetName);
                XLSX.writeFile(wb, fileName);
            }
        </script>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:xlsx浏览器导出

          本文链接:https://www.haomeiwen.com/subject/nurejltx.html