【问题标题】:how to export html table to excel, with pagination如何将html表格导出到excel,带分页
【发布时间】:2018-04-06 20:27:59
【问题描述】:

我有一个表单,它以 HTML 表的形式显示 mysql 数据,每页使用 5 条记录的分页。我正在尝试将我的表导出到 excel,但它仅导出当前显示的行的数据,这意味着如果有 20 行,它只显示前 5 行。我怎样才能让它导出那些没有显示但仍然是搜索的一部分?我看到其他几个人也有类似的问题,但没有一个人有答案(How to export all HTML table rows to Excel file from a paginated table?)。希望我能得到一个!

<?php
if(isset($_GET['submit']) || isset($_GET['page'])){
    // Setting up the Pagination below

    echo '<center>';
$page_query = "SELECT * FROM tbl_call_log_detail ";
$page_query .= "WHERE
     (dealer_id = '$call_id' AND '$endDate'='1970-01-01' AND '$startDate' ='1970-01-01')
  OR ( Time <= '$endDate' AND Time >= '$startDate'  
                    AND (dealer_id = '$call_id' OR'$call_id'='' ))    
  OR ('$endDate'='1970-01-01' AND '$startDate' ='1970-01-01'  AND '$call_id'='') ";
$page_result = mysqli_query($conn, $page_query);
$total_records = mysqli_num_rows($page_result);
$total_pages = ceil($total_records/$record_per_page);
$start_loop = $page;
$difference = $total_pages - $page;

if($difference <= $total_pages){
    $start_loop = $total_pages - $difference;
}
$end_loop = $start_loop + 2;

if($end_loop > $total_pages){
    $end_loop = $total_pages;
}
if($difference > $total_pages){
    $end_loop = $total_pages;
}
echo '<div class = "center">';
echo '<div class = "pagination">';
if($page > 1){
    echo "<a href= 'dealer_call_log.php?page=1".$urlparameter."'>First</a>";
    echo "<a href= 'dealer_call_log.php?page=".($page - 1).$urlparameter."'> << </a>";
}
for ($i = $start_loop; $i <= $end_loop; $i++){
    echo "<a href= 'dealer_call_log.php?page=".$i.$urlparameter."'>".$i."</a>";
}
if($page < $end_loop){
    echo "<a href= 'dealer_call_log.php?page=".($page + 1).$urlparameter."'> >> </a>";
    echo "<a href= 'dealer_call_log.php?page=".$total_pages.$urlparameter."'>Last</a>";
}
 if($page < 1){
$page = 1;
 }
    echo '</div>'; 
    echo '</div>';

    echo '<br>';

    $sql = "SELECT Name, SFID, Comment, Time FROM tbl_call_log_detail 
    WHERE
     (dealer_id = '$call_id' AND '$endDate'='1970-01-01' AND '$startDate' ='1970-01-01')
  OR ( Time <= '$endDate' AND Time >= '$startDate'  
                    AND (dealer_id = '$call_id' OR'$call_id'='' ))    
  OR ('$endDate'='1970-01-01' AND '$startDate' ='1970-01-01'  AND '$call_id'='')
     ORDER BY Time DESC LIMIT $start_from, $record_per_page ";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_num_rows($result);
    $all_property = array();

    echo "<table class = 'data-table' border = '1' cellpadding = '9' bgcolor = '#CCCCCC' id = 'data-table'>
            <tr class = 'data-heading'>";
    while($property = mysqli_fetch_field($result)){
        echo '<td><b> '. $property ->name. ' </b></td>';
        array_push($all_property, $property ->name);

    }
    echo '</tr>';

    while ($row = mysqli_fetch_array($result)){
        echo '<tr>';
        foreach($all_property as $item){
            echo '<td> '. $row[$item] . ' </td>';
        }
        echo '</tr>';
        echo '</center>';

            }
            echo '</table>';
            echo '<br>';
            ?>

// This is what is getting the current rows, but not all

            <input type = "submit" onclick = "window.open('data:application/vnd.ms-excel, '+encodeURIComponent(document.getElementById('data-table').outerHTML));" value = "Export into excel" />  
            <?php 
}
?>

更新:找到了我正在寻找的答案我只是运行了一个没有 LIMIT 子句的新 sql 查询,并将其存储在一个隐藏的表中。然后我使用隐藏表导出数据

// SQL and hidden table for exporting to excel 
            $page_query2 = "SELECT * FROM tbl_call_log_detail ";
            $page_query2 .= "WHERE
     (dealer_id = '$call_id' AND '$endDate'='1970-01-01' AND '$startDate' ='1970-01-01')
  OR ( Time <= '$endDate' AND Time >= '$startDate'  
                    AND (dealer_id = '$call_id' OR'$call_id'='' ))    
  OR ('$endDate'='1970-01-01' AND '$startDate' ='1970-01-01'  AND '$call_id'='') ORDER BY TIME DESC ";

  $page_result2 = mysqli_query($conn, $page_query2);
  $row2 = mysqli_num_rows($page_result2);
  $all_property2 = array();

  echo "<table class = 'data-table2' border = '1' cellpadding = '9' bgcolor = '#CCCCCC' id = 'data-table2' hidden>
            <tr class = 'data-heading2'>";
    while($property = mysqli_fetch_field($page_result2)){
        echo '<td><b> '. $property ->name. ' </b></td>';
        array_push($all_property2, $property ->name);

    }
    echo '</tr>';

    while ($row2 = mysqli_fetch_array($page_result2)){
        echo '<tr>';
        foreach($all_property2 as $item){
            echo '<td> '. $row2[$item] . ' </td>';
        }
        echo '</tr>';
        echo '</center>';

            }
            echo '</table>';
            ?>
            <input type = "submit" onclick = "window.open('data:application/vnd.ms-excel, '+encodeURIComponent(document.getElementById('data-table2').outerHTML));" value = "Export into excel" />
            <?php 
}
?>

【问题讨论】:

    标签: javascript php excel


    【解决方案1】:

    你可以使用 JQuery table2excel 插件,下面是链接

    http://www.jqueryscript.net/table/Export-Html-Table-To-Excel-Spreadsheet-using-jQuery-table2excel.html
    

    尝试将以下代码笔仅用于 JavaScript

    https://codepen.io/kostas-krevatas/pen/mJyBwp
    

    【讨论】:

    • 有什么办法只用javascript来做吗?
    • 不,仍然没有导出所有行。只显示那些
    • 你只想用 JS 来做
    • 是的。我再次检查了代码笔,它没有这样做
    猜你喜欢
    • 1970-01-01
    • 2014-08-29
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    相关资源
    最近更新 更多