2014-04-29 118 views
0

我試圖將我的數據導出到csv文件並讓用戶下載它。我使用fputcsv()函數,但在文件中,數據寫入單個單元而不是相鄰單元。我不知道是什麼問題。請幫幫我。這裏是我的代碼fputcsv - 函數給出錯誤。

session_start(); 
header('Content-Type: application/csv'); 
header('Content-Disposition: attachement; filename=report_'.time().'.csv;'); 
$data = $_SESSION['data']; 
$file = fopen('php://output','w'); 
foreach($data as $i=>$value) 
{ 
    fputcsv($file, $value,";"); 
} 
fclose($file); 

,這是文件的樣子..

and this is how the file looks like..

+0

試試這個fputcsv($ FP,array_values($列表), ';','「) ; – dkakoti

回答

1

嘗試像

$data = array (
    'aaa,bbb,ccc,dddd', 
    '123,456,789', 
    '"aaa","bbb"'); 
$fp = fopen('php://output', 'w+'); 
header('Content-type: application/octet-stream'); 
header('Content-disposition: attachment; filename="data.csv"'); 
foreach($data as $line){ 
    $val = explode(",",$line); 
    fputcsv($fp, $val); 
} 
fclose($fp); 

,或者嘗試,可以根據您的數據

foreach($data as $i=>$value) 
{ 
    fputcsv($file, $value); 
} 
+0

仍然一樣,無論如何感謝。 – Vignesh

+0

根據您的數據更改 –

+0

檢查我的更新回答' –

1

使用,而不是;delimiter

fputcsv($file, $value, ",");更換fputcsv($file, $value,";");並嘗試

例子:

header('Content-Type: application/csv'); 
header('Content-Disposition: attachement; filename=report_'.time().'.csv;'); 
$data = array(array('one' => 1, 'two' => 2)); 
$file = fopen('php://output','w'); 
foreach($data as $i => $value) 
{ 
    //fputcsv($file, $value, ";"); 
    fputcsv($file, $value, ","); 
} 
fclose($file);