2012-05-14 22 views
-1

我的功能有什麼遺漏在我的代碼,而出口從PHP

function xlstest(){ 
      $headers='SN'."\t".'Reservation ID'."\t".'Hotel Name'."\t".'Customer Name'; 
      $data='1'."\t".'234'."\t".'My hotel name'."\t".'Jonny'; 
      $filename='testing'; 
      header("Content-type: application/octet-stream"); 
      header("Content-Disposition: attachment; filename=".$filename.".xls"); 
      header("Pragma: no-cache"); 
      header("Expires: 0"); 
      echo "$headers\n$data"; 
     } 

擅長同時呼籲在管理面板此功能將導出到Excel文件,但不具有上述的頭部和數據,但它顯示所有出現在管理面板中的文本 enter image description here

有誰知道爲什麼會發生這種情況?

回答

0

我的問題解決了。其實我已經叫這個功能僅低於<link rel="stylesheet" href=""/>

<script type="text/javascript" src=".... .."></script> 

一旦我搬到這些CSS和腳本..我的問題就解決了以上怪.......

0

您根本沒有使用 api。請閱讀documentation並嘗試使用api。

關於您正在使用的代碼,使用\t(空格前後)分隔字段。 此外,而不是octet-stream使用excels頭文件。

header('Content-type: application/ms-excel'); 

用法:

function xlstest(){ 
    $headers='SN'." \t ".'Reservation ID'." \t ".'Hotel Name'." \t ".'Customer Name'; 
    $data='1'." \t ".'234'." \t ".'My hotel name'." \t ".'Jonny'; 
    $filename='testing'; 
    header("Content-type: application/ms-excel"); 
    header("Content-Disposition: attachment; filename=".$filename.".xls"); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    echo "$headers\n$data"; 
} 

更新

要導出CSV文件,你可以做到以下幾點。它幾乎相同,但分隔符是逗號,而內容類型是application/vnd.ms-excel

function csvtest(){ 
    $headers='SN'.",".'Reservation ID'.",".'Hotel Name'.",".'Customer Name'; 
    $data='1'.",".'234'.",".'My hotel name'.",".'Jonny'; 
    $filename='testing'; 
    header("Content-type: application/vnd.ms-excel"); 
    header("Content-Disposition: attachment; filename=".$filename.".csv"); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    echo "$headers\n$data"; 
} 
+0

我以前只是通過出口這些標題..這次我不知道什麼是probelm ..讓我檢查空間問題... – sujal

+0

@sujal,而且,它將很容易導出數據爲'.csv'文件,並稍後訪問它通過, excel,它基本上是一樣的東西。 – Starx

+0

我應該如何改變.csv?只是改變標題將工作?> – sujal