簡體   English   中英

如何使用jQuery導出帶有分頁的表格到excel

[英]how to use jQuery to export table with pagination to excel

我如何使用jQueryJavaScripttable pagination數據pagination導出到Excel。

我嘗試此代碼

<script type="text/javascript">
        var tableToExcel = (function() {
            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 function(table, name) {
                if (!table.nodeType) table = document.getElementById(table)
                var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
                window.location.href = uri + base64(format(template, ctx))
            }
        })()
    </script>

但是我只能導出分頁的一頁,而我想導出表中的所有數據。 我能做什么??

  1. 制作文件export_data.php
  2. 將查詢條件放在export_data.php中

例如

$link = mysqli_connect("localhost", "my_user", "my_password", "my_db");
$sql = "SELECT * FROM `users` WHERE 1";
$result = mysqli_query($link, $sql);

if (mysqli_num_rows($result) > 0) {
    $file_name = 'All Users.csv';
    header('Content-Type: text/csv; charset=utf-8');
    header("Content-Disposition: attachment; filename = $file_name");
    $output = fopen('php://output', 'w');

    foreach ($result as $row) {
        fputcsv($output, $row);
    }
    fclose($output);
}
  1. 向ajax請求到export_data.php

這應該為您提供完整的數據。 您需要根據需要更改sql。

通過刪除查詢中的限制來確保腳本獲取所有數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM