PHP导出CSV

1390阅读 0评论2017-12-09 nuoyazhou110
分类:PHP


点击(此处)折叠或打开

  1. $header = array(
  2.             'id' => 'ID',
  3.             'create_time' => '提交时间'
  4.         );
  5. header('Content-Type: application/vnd.ms-excel');
  6.         header('Content-Disposition: attachment;filename="明细.csv"');
  7.         header('Cache-Control: max-age=0');
  8.         $fp = fopen('php://output', 'a');
  9.         //导出表头
  10.         $header_arr = array();
  11.         foreach( $header as $key => $val ) {
  12.             $header_arr[] = mb_convert_encoding($val,'gbk','utf8');
  13.         }
  14.         fputcsv($fp, $header_arr);

  15.         $start = 0;
  16.         $offset = 5000;
  17.         if( $count['num'] > 0 ) {
  18.             while( true ) {
  19.                 ob_flush();
  20.                 flush();
  21.                 $args['start'] = $start;
  22.                 $args['offset'] = $offset;
  23.                 $info = $this->get_list($args);
  24.                 if( !empty($info) ) {
  25.                     foreach( $info as $row ) {
  26.                         $one_row = array();
  27.                         foreach( $header as $key => $val ) {
  28.                             $one_row[]=mb_convert_encoding((string)$row[$key],'gbk','utf8');
  29.                         }
  30.                         fputcsv($fp, $one_row);
  31.                     }
  32.                 } else {
  33.                     break;
  34.                 }
  35.                 $start +=5000;
  36.             }
  37.         }

上一篇:phpexcel迭代导入数据
下一篇:JAVA多线程文件拷贝