2013-03-19 15 views
26

Ich möchte eine CSV-Datei erstellen, aber wenn ich den Code ausführen, gibt es eine Bankseite und keine CSV-Datei zurück. Ich benutze PHP 5. Ich verwende den folgenden Code ein:Erstellen von CSV-Datei mit PHP

<?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); 
?> 

Vielen Dank!

+1

Sie müssen die Ausgabe nach 'fclose ($ fp);' echo' zurückgeben. ' –

+0

Sie" echo "nichts so die Seite wird nur leer sein. Es wird nur csv für Sie nach Ihrem Code erstellen. – Rikesh

Antwort

56

Es ist leer, weil Sie auf file schreiben. Sie sollten stattdessen output mit php://output schreiben und auch Header-Informationen senden, um den csv anzuzeigen.

Beispiel

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

Der Inhaltstyp sollte Text sein/csv nicht application/excel – Scott

19

@ Antwort Babas ist groß. Aber warum explode als fputscv Array als Parameter verwenden?

Zum Beispiel, wenn Sie ein drei Spalten, vier Zeilen Dokument, hier ist eine weitere gerade Ausführung:

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

Schön. Sollte der Content-Type nicht text/csv sein? – Adam

+0

Definitiv, das sollte funktionieren und vielleicht sogar passender sein. Mehr Details [hier] (http://stackoverflow.com/questions/393647/response-content-type-as-csv) – Buzut

0

ich das Beispiel hoffen für Sie nützlich sein wird

$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(); 
Verwandte Themen