2010-09-18 34 views
0

我試圖打開一個文件作爲excel導出。我可以看到螢火蟲中的資料,但沒有選擇提供該檔案給使用者。我在這裏錯過了什麼?我已經包含了我的代碼來處理查詢等,並歡迎您的意見。非常感謝輸出數據爲excel文件

header("Content-Type: application/force-download"); 
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); 
header("Content-type: application/vnd.ms-excel; name='excel'"); 
header("Content-Disposition: attachment; filename=".$_GET['e'].".xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
$e = $_GET['e']; 
// Load the common classes 
require_once('../../../includes/common/KT_common.php'); 

// Load the tNG classes 
require_once('../../../includes/tng/tNG.inc.php'); 

// Make a transaction dispatcher instance 
$tNGs = new tNG_dispatcher("../../../"); 

// Make unified connection variable 
$conn_conn= new KT_connection($conn, $database_conn); 


    switch($e) { 


     case "act": 
      mysql_select_db($database_conn, $conn); 

     $select = "SELECT service, activity, company, user, item, filebox, date FROM act"; 
     $export = mysql_query($select); 
     $count = mysql_num_fields($export); 
     for ($i = 0; $i < $count; $i++) { 
     $header .= mysql_field_name($export, $i)."\t"; 
     } 
     while($row = mysql_fetch_row($export)) { 
     $line = ''; 
     foreach($row as $value) { 
     if ((!isset($value)) OR ($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 = "\n(0) Records Found!\n"; 
     } 
     print "$header\n$data"; 

     break; 
+1

你有3個內容類型的標題,每個不同。你基本上是說「這個文件是一個鬆餅,這個文件是一盆沸水,這個鍋也是一塊口袋裏的絨毛」。除非你經常做口袋絨毛鬆餅湯,否則這不可能是真的。 – 2010-09-18 15:10:40

回答

1

真正的問題是,你需要在你的內容處置標題引用的文件名,但你只需要一個Content-Type頭,以及一些下面列出的其它緩存頭的應該幫助與IE瀏覽器通過SSL潛在問題(如果使用)

header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past 
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified 
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 
header ('Pragma: public'); // HTTP/1.0 
header ('Content-Type: application/vnd.ms-excel'); 
header ('Content-Disposition: attachment; filename="'.$_GET['e'].'.xls"'); 
header ('Content-Transfer-Encoding: binary'); 

作爲一個值得注意的一點:你不是實際創建一個Excel XLS文件,只是一個製表符分隔值文件具有.xls擴展名

+0

謝謝你馬克。另外,喜歡描述性的Marc B :-) – 2010-09-19 16:25:53