如何通过分页将html表导出为ex​​cel

时间:2017-10-25 16:31:23

标签: javascript php excel

我有一个表格,它以HTML表格的形式显示mysql数据,每页分页5个记录。我试图将我的表导出为ex​​cel,但它只导出当前显示的行的数据,这意味着如果有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 
}
?>

1 个答案:

答案 0 :(得分: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