简体   繁体   中英

Export Excel from Mysql Table

Pls, let someone help me with this code, When I export, it export the file but start with row2, the first row is excluded.it reads like , header, then row2,3,4 till the end of the row.

<?php
        require_once("/includes/session.php");
         require_once("/includes/db_connection.php");
         require_once("/includes/functions.php");
        // Table Name that you want
        // to export in csv
        $ShowTable = "staff_tab";
         $today=date("dmY");
        $FileName = "StaffRecord".$today.". csv";
        $file = fopen($FileName,"w");

        $sql = mysqli_query($connection,("SELECT * FROM $ShowTable LIMIT 500"));
        $row = mysqli_fetch_assoc($sql);
        // Save headings alon
        $HeadingsArray=array();
        foreach($row as $name => $value){
        $HeadingsArray[]=$name;
        }
        fputcsv($file,$HeadingsArray);
        // Save all records without headings

        while($row = mysqli_fetch_assoc($sql)){
        $valuesArray=array();
        foreach($row as $name => $value){
        $valuesArray[]=$value;
        }
        fputcsv($file,$valuesArray);
        }
        fclose($file);

        header("Location: $FileName");

        echo "Complete Record saves as CSV in file: <b style=\"color:red;\">$FileName</b>";
        ?>

Your call to $row = mysqli_fetch_assoc($sql); is pushing the internal pointer forward to row 2. Use mysqli_data_seek($sql, 0); to push it back to the start. http://php.net/manual/en/function.mysql-data-seek.php

    ...
    $sql = mysqli_query($connection,("SELECT * FROM $ShowTable LIMIT 500"));
    $row = mysqli_fetch_assoc($sql);
    mysqli_data_seek($sql, 0);           // return pointer to first row
    // Save headings alon
    $HeadingsArray=array();
    ...

maybe this could help as this will include all rows and set headers from the db headers. This would also force download the file without leaving the page.

$export = mysqli_query ($query) or die ( "Sql error : " . mysqli_error( ) );

// extract the field names for header
$fields = mysqli_num_fields ( $export );

for ( $i = 0; $i < $fields; $i++ )
{

    $headers = mysqli_fetch_field($export);
    $header .= $headers->name. "\t";
}

// export data
while( $row = mysqli_fetch_row( $export ) )
{
    $line = '';
    foreach( $row as $value )
    {
        if ( ( !isset( $value ) ) || ( $value == "" ) )
        {
            $value = "\t";
        }
        else
        {
            $value = str_replace( '"' , '""' , $value );
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );

if ( $data == "" )
{
    $data = "\nNo Record(s) Found!\n";
}

// allow exported file to download forcefully
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Something.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM