php
  • csv
  • export-to-excel
  • 2017-04-14 68 views 0 likes 
    0

    我想打印爲excel作爲csv。這是我迄今爲止的答案。打印語法輸出爲csv

    <?php 
    
    $dbh = dbh_get(); 
    
    header('Content-type: application/csv'); 
    header("Content-Disposition: inline; filename=file.csv"); 
    readfile('file.csv'); 
    
    $sql = 'select t.* 
          from old t 
         where t.entered >= ? and t.entered < ? 
          and t.status <> \'cancel\' 
          and t.ccy = \'USD\' 
         order by id desc'; 
    $stmt = $dbh->prepare($sql); 
    
    $v = array(); 
    $v[] = $date->format(DateTime::ISO8601); 
    $v[] = $dateEnd->format(DateTime::ISO8601); 
    
    
    $numRows = 0; 
    $stmt->execute($v); 
    while (true) { 
        $r = $stmt->fetch(); 
        if (is_bool($r)) break; 
        $numRows++; 
    
        $bop = $r['comments']; 
        $bop1 = strtok($bop, " "); //remove all but first word 
        $bop2 = substr(strstr($bop," "), 1); //remove first word and space 
    
    $headers = ['PLACE','YEAR','MONTH','COY','NT','TOPS','BOP','COUNTRY','REC','SENT','DESC']; 
    $fields = ["Old",2015,$textDate,$r['cols'],$r['nt'],$bop1,"NTR",$r['name'],$r['first_name'],$r['last_name'],$bop2]; 
    
    $fp = fopen('file.csv', 'w'); 
    fputcsv($fp, $headers); 
    fputcsv($fp, $fields); 
    fclose($fp); 
    } 
    
    dbh_free($dbh); 
    

    仍然不能使用這個我得到一行代碼打印到屏幕上,沒有下載。我需要的是下載csv文件,以便我可以用excel打開它。

    +0

    如何撤消所有編輯並返回到原始問題,我沿着錯誤的軌道 – Kilisi

    回答

    1

    不要通過手動構建CSV來懲罰自己。改爲使用PHP's built in functionality

    <?php 
    
    $bop1 = 'bop1'; 
    $bop2 = 'bop2'; 
    $textDate = '2017-04-13'; 
    $r = [ 
        'ccy' => 'ccy', 
        'amount' => 'amount', 
        'name' => 'name', 
        'first_name' => 'first_name', 
        'last_name' => 'last_name', 
    ]; 
    
    $headers = ['column1','column2','column3','column4','column5','column6','column7','column8','column9','column10','column11']; 
    $fields = ["Old","2015",$textDate,$r['ccy'],$r['amount'],$bop1,"NTR",$r['name'],$r['first_name'],$r['last_name'],$bop2]; 
    
    $fp = fopen('file.csv', 'w'); 
    fputcsv($fp, $headers); 
    fputcsv($fp, $fields); 
    fclose($fp); 
    
    header('Content-type: application/csv'); 
    header("Content-Disposition: inline; filename=file.csv"); 
    readfile('file.csv'); 
    

    下面是使用更新後的代碼更新的例子:

    <?php 
    
    $dbh = dbh_get(); 
    
    $sql = 'select t.* 
          from old t 
         where t.entered >= ? and t.entered < ? 
          and t.status <> \'cancel\' 
          and t.ccy = \'USD\' 
         order by id desc'; 
    $stmt = $dbh->prepare($sql); 
    
    $v = array(); 
    $v[] = $date->format(DateTime::ISO8601); 
    $v[] = $dateEnd->format(DateTime::ISO8601); 
    $stmt->execute($v); 
    
    $fp = fopen('file.csv', 'w'); 
    $headers = ['PLACE','YEAR','MONTH','COY','NT','TOPS','BOP','COUNTRY','REC','SENT','DESC']; 
    fputcsv($fp, $headers); 
    
    while ($r = $stmt->fetch()) { 
        $bop = $r['comments']; 
        $bop1 = strtok($bop, " "); //remove all but first word 
        $bop2 = substr(strstr($bop," "), 1); //remove first word and space 
    
        $fields = ["Old",2015,$textDate,$r['cols'],$r['nt'],$bop1,"NTR",$r['name'],$r['first_name'],$r['last_name'],$bop2]; 
        fputcsv($fp, $fields); 
    } 
    
    fclose($fp); 
    dbh_free($dbh); 
    
    header('Content-type: application/csv'); 
    header("Content-Disposition: inline; filename=file.csv"); 
    readfile('file.csv'); 
    
    +0

    試圖解決這個問題,如果我使用了這個,我會如何將標題放入?還有一些數據來自SQL語句,並且會有數千行 – Kilisi

    +1

    您可以使用循環將每行添加到文件。頭文件只是對fputcsv()的另一個調用。我會更新答案以表明這一點。 –

    +0

    我無法得到它的工作,它不下載任何東西。 – Kilisi

    0

    你可以做這樣的事情:

    $data = ['Old', '2015', $textDate]; 
        file_put_contents('pathTofile.csv', implode(';', $data) . PHP_EOL, FILE_APPEND); 
    

    更新:

    現在是一些代碼更在這裏,相比貼出的一班。只需將Content-Disposition標題更改爲:

    header('Content-Disposition: attachment; filename="'.basename('file.csv').'"'); 
    

    它強制下載。請參閱Content-DispositionRead File

    +0

    爲什麼當你可以使用PHP內置的支持來構建CSV文件? –

    +0

    你的w標誌有一個問題,它會在寫入文件之前清除文件。有了這種方法,你可以選擇你是分隔符。 MS Excel預計;作爲分隔符 – jUnG3

    +0

    1.文件的打開不會在循環中,因爲這會很慢且不必要。 2.根據定義,CSV文件*具有用逗號分隔的字段,Excel可以很好地處理這些字段。 –

    相關問題