简体   繁体   中英

export mysql to xls using php (browser issue)

ok there is a few issues when i downloaded it using mozila firefox, name of file is just "report" and without .xls . so i must click the openwith first to open the file. but this issue is not happen when i run it in chrome.

mozila : report chrome : report 29-08-2014 dailymeal.xls

can you help me to tell what is wrong in my code ? thanks in advance

here is the code :

<?php



$con = mysql_connect('localhost','root','');
mysql_select_db('qrgad',$con);


$tanggal_awal=$_POST['tanggal_awal'];
$tanggal_akhir=$_POST['tanggal_akhir'];
$today= date ("Y-m-d");
$host = "localhost"; 
$user = "root"; 
$password = ""; 
$db_name = "qrgad"; 
$tbl_name =$_POST['report'];


if($tbl_name=="dailymeal")
 {$query = "SELECT * FROM $tbl_name where date>='$tanggal_awal' and date <='$tanggal_akhir'"; }
if($tbl_name=="infomeal")
{ $query = "SELECT * FROM $tbl_name where tanggal >='$tanggal_awal' and tanggal <='$tanggal_akhir'"; }
if($tbl_name=="keluhan")
{ $query = "SELECT * FROM $tbl_name where tlapor >='$tanggal_awal' and tlapor <='$tanggal_akhir'"; }
if($tbl_name=="perjalanan")
{ $query = "SELECT * FROM $tbl_name where request_date>='$tanggal_awal' and request_date <='$tanggal_akhir'"; }
if($tbl_name=="tamu")
{ $query = "SELECT * FROM $tbl_name where jam_masuk>='$tanggal_awal' and jam_masuk <='$tanggal_akhir'"; }
if($tbl_name=="tiket")
{ $query = "SELECT * FROM $tbl_name where waktu_input>='$tanggal_awal' and waktu_input <='$tanggal_akhir'"; }
if($tbl_name=="trx_kons")
{ $query = "SELECT * FROM $tbl_name where date_trx>='$tanggal_awal' and date_trx <='$tanggal_akhir'"; }
if($tbl_name=="uniform")
{ $query = "SELECT * FROM $tbl_name where reqtime >='$tanggal_awal' and reqtime <='$tanggal_akhir'"; }
if($tbl_name=="sewa")
{ $query = "SELECT * FROM $tbl_name where waktu_input >='$tanggal_awal' and waktu_input <='$tanggal_akhir'"; }
if($tbl_name=="komunikasi")
{ $query = "SELECT * FROM $tbl_name where waktu_input >='$tanggal_awal' and waktu_input <='$tanggal_akhir'"; }
if($tbl_name=="konsumable")
{ $query = "SELECT * FROM $tbl_name"; }

$header = '';
$data ='';
$export = mysql_query ($query ) or die ( "Sql error : " . mysql_error( ) );


$fields = mysql_num_fields ( $export );

for ( $i = 0; $i < $fields; $i++ )
{
    $header .= mysql_field_name( $export , $i ) . "\t";
}


while( $row = mysql_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";                        
}


header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$today." Report ".$tbl_name.".xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";

?>

Change this line: (it's the quotes) see ^ here and ^ here under the first line of code below.

header("Content-Disposition: attachment; filename=".$today." Report ".$tbl_name.".xls");
                                                           ^ here   ^ and here

to either (and by removing the quotes)

header("Content-Disposition: attachment; filename=".$today.Report.$tbl_name.".xls");

or

header("Content-Disposition: attachment; filename=".$today.'_' .Report.'_'.$tbl_name.".xls");

The last one will put an underscore between each name/word.


Edit: as per OP's comment

thanks guys, it's resolved. the problem is header("Content-type: application/octet-stream"); and that should be header("Content-Type: application/vnd.ms-excel;"); and thanks for the filename issues

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