0

我正在尝试将我的数据导出到 csv 文件中并让用户下载它。我正在使用 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);

这就是文件的样子..

这就是文件的样子..

4

2 回答 2

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);
}
于 2014-04-29T11:52:33.777 回答
1

使用,而不是;fordelimiter

替换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);
于 2014-04-29T11:55:09.573 回答