Skip to content

Instantly share code, notes, and snippets.

@sgsheg
Created August 13, 2021 06:55
Show Gist options
  • Save sgsheg/6bf005602fef9a68f78fff5fa567a2e6 to your computer and use it in GitHub Desktop.
Save sgsheg/6bf005602fef9a68f78fff5fa567a2e6 to your computer and use it in GitHub Desktop.

Revisions

  1. sgsheg created this gist Aug 13, 2021.
    31 changes: 31 additions & 0 deletions php-export-csv.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    public function put_csv($list,$title){
    $file_name="CSV".date("mdHis",time()).".csv";
    header ( 'Content-Type: application/vnd.ms-excel' );
    header ( 'Content-Disposition: attachment;filename='.$file_name );
    header ( 'Cache-Control: max-age=0' );
    $file = fopen('php://output',"a");
    $limit=1000;
    $calc=0;
    foreach ($title as $v){
    $tit[]=iconv('UTF-8', 'GB2312//IGNORE',$v);
    }
    fputcsv($file,$tit);
    foreach ($list as $v){
    $calc++;
    //等于1000时刷新缓存
    if($limit==$calc){
    ob_flush();
    flush();
    $calc=0;
    }
    //根据头部内容顺序插入
    foreach ($title as $key => $value) {
    $tarr[]=iconv('UTF-8', 'GB2312//IGNORE',$v[$key]);
    }
    fputcsv($file,$tarr);
    unset($tarr);
    }
    unset($list);
    fclose($file);
    exit();
    }