2013-03-19 31 views
26

Bir csv dosyası oluşturmak istiyorum, ancak kodu çalıştırdığımda bir banka sayfası ve hiçbir csv dosyası döndürmez.php ile csv dosyası oluşturma

<?php 
    $data = array ('aaa,bbb,ccc,dddd', 
        '123,456,789', 
        '"aaa","bbb"'); 

    $fp = fopen('data.csv', 'w'); 
    foreach($data as $line){ 
      $val = explode(",",$line); 
      fputcsv($fp, $val); 
    } 
    fclose($fp); 
?> 

teşekkür ederiz: PHP'yi 5. Ben şu kodu kullanın kullanın!

+1

için yararlı olacağını umuyoruz: Eğer üç sütun varsa Örneğin

, dört satır belge, burada daha düz versiyonu Fclose ($ fp) 'den sonra çıkışı' echo 'yapmalısınız; ' –

+0

“ Echoing ”değilsiniz, böylece sayfa sadece boş olacaktır. Sadece sizin kodunuza göre sizin için csv yaratacaktır. – Rikesh

cevap

56

Boş çünkü file'a yazıyorsunuz. bunun yerine php://output kullanarak output'a yazmalı ve ayrıca csv'sini belirtmek için başlık bilgilerini göndermelisiniz. Baba'nın cevabı @

Örnek

header('Content-Type: application/excel'); 
header('Content-Disposition: attachment; filename="sample.csv"'); 
$data = array(
     'aaa,bbb,ccc,dddd', 
     '123,456,789', 
     '"aaa","bbb"' 
); 

$fp = fopen('php://output', 'w'); 
foreach ($data as $line) { 
    $val = explode(",", $line); 
    fputcsv($fp, $val); 
} 
fclose($fp); 
+5

İçerik Türü, text/csv not application/excel – Scott

19

harika. Ama neden explode'u fputscv olarak kullanmak diziyi parametre olarak alır?

header('Content-Type: application/excel'); 
header('Content-Disposition: attachment; filename="sample.csv"'); 

$user_CSV[0] = array('first_name', 'last_name', 'age'); 

// very simple to increment with i++ if looping through a database result 
$user_CSV[1] = array('Quentin', 'Del Viento', 34); 
$user_CSV[2] = array('Antoine', 'Del Torro', 55); 
$user_CSV[3] = array('Arthur', 'Vincente', 15); 

$fp = fopen('php://output', 'w'); 
foreach ($user_CSV as $line) { 
    // though CSV stands for "comma separated value" 
    // in many countries (including France) separator is ";" 
    fputcsv($fp, $line, ','); 
} 
fclose($fp); 
+1

Nice olmalıdır. İçerik Türü metin/csv değil mi? – Adam

+0

Kesinlikle, bu işe yarayacak ve hatta daha uygun olabilir. Daha fazla detay [burada] (http://stackoverflow.com/questions/393647/response-content-type-as-csv) – Buzut

0

i örnek

$data= "Sr. No.,Vendor Name,Cargo Type,Amount,Delivery Address,Pickup Time,Delivery Time\n"; 

     $i = 1; 
     if(!empty($detail)) { 
      foreach ($detail as $list) { 

       $data .= $i.","; 
       $data .= any data 
       $data .= any data 
       $data .= any data 
       $data .= any data 
       $data .= any data 
       $data .= any data 
       $data .="\n"; 
       $i++; 

      } 
     } 

     header("Content-Type: application/csv"); 
     $csv_filename = 'anyfilename.xlsx'; 
     header("Content-Disposition:attachment;filename=".$csv_filename); 
     $fd = fopen ($csv_filename, "w"); 
     fputs($fd,$data); 
     fclose($fd); 
     echo $data; 
     die();