2013-03-14 21 views
0

我有一個頁面,顯示所有數據庫的過濾器功能..和過濾器後,我想下載到excel甲酸...我sucessfull獲取所有的數據和fwrite()它..現在請解釋我怎麼下載到Excel工作表

http://sms.weddingsplanner.co.in/all_inquiry.php部份是我的網頁上EXCELL按鈕出現..

 <?php 
       include 'functions.php'; 
       ob_start(); 




    if($_POST['types'] == 'name') { 

      $query = "SELECT * FROM visitor_detail WHERE name='".$_POST['filter']."'        ORDER BY id  DESC "; 
    } 
         elseif($_POST['types'] == 'mobile') { 

      $query = "SELECT * FROM visitor_detail WHERE mobile='".$_POST['filter']."' ORDER BY id DESC "; 
     } 
       elseif($_POST['types'] == 'OccasionType') { 

    $query = "SELECT * FROM visitor_detail WHERE OccasionType='".$_POST['filter']."' ORDER BY id DESC "; 
      } 

     elseif($_POST['types'] == 'InquiryDate') { 
    $query="SELECT * FROM visitor_detail WHERE TodayDate between '".$_POST['From']."' and '".$_POST['TO']."' ORDER BY id DESC " ; 
    } 

    elseif($_REQUEST['types'] == 'OccasionDate') { 
        $que="SELECT * FROM visitor_detail WHERE date between  '".$_REQUEST['From']."' and '".$_REQUEST['To']."' ORDER BY id DESC "; 
        } 
    else { 
       $query = "SELECT * FROM visitor_detail ORDER BY id DESC "; 
      } 

    $sql = mysql_query($query); 


       $line1="Id\tgender\tname\tmobile\taddress\temail\totherno\tzipcode\tOccasionType\tdate\tgue  st\tdescription\tTodayDate\tauther\t"; 
      $data="$line1\n"; 
      $br = '<br>'; 

     $fileName = "./my.txt"; 
$file = fopen($fileName, "w"); 
fwrite($file, $data."\n"); 
     $i=1; 
    while($row = mysql_fetch_assoc($sql)) 
{ 
      $line2=$i . "\t" . $row['gender'] . "\t" . $row['name'] . "\t" .       $row['mobile'] . "\t" . $row['address'] . "\t" . $row['email'] . "\t" . $row['otherno'] . "\t" .  $row['zipcode'] . "\t" . $row['OccasionType'] . "\t" . $row['date'] . "\t"        . $row['guest'] . "\t" . $row['description'] . "\t" . $row['TodayDate'] . "\t" . $row['auther'] . "\t"; 
     $data1=$line2.$br; 
     echo $data1; 
       fwrite($file, $data1); 
       $i++; 
    } 
       fclose($file); 

    ?> 
+0

你只是想知道如何下載CSV文件(擺脫回波,併發送相應的報頭)或如何創建一個真正的Excel文件? – 2013-03-14 11:45:21

+0

我假定你的意思是[CSV](http://en.wikipedia.org/wiki/Comma-separated_values)文件。如果是這樣,你有[fputcsv](http://php.net/manual/en/function.fputcsv.php) – juco 2013-03-14 11:45:45

回答

0

蠻力服務器上的所有CSV的下載,你可以添加到您的.htaccess文件:

AddType application/octet-stream pdf 

EDIT(你的PHP文件中內嵌的解決方案會更喜歡這樣):

header('Content-Type: application/pdf'); 
header('Content-Disposition: attachment; filename=example.pdf'); 
header('Pragma: no-cache'); 
readfile("/path/to/yourfile.pdf"); 
+0

@ hardik ..感謝它的工作..但它顯示在我的CSV文件非常糟糕的同學怎麼可以我讓它下載它correcr甲酸鹽? – 2013-03-14 12:14:41

+0

它的工作正常......但是如果我想將它下載到pdf格式文件中 – 2013-03-15 12:16:53

+0

我已更新我的代碼請嘗試 – Hardik 2013-03-16 06:17:59