1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: hanks 5 * Date: 2016/4/20 6 * Time: 13:51 7 */ 8 9 namespace Home\Controller; 10 use Think\Controller; 11 12 class CsvController extends Controller 13 { 14 //导出csv文件 15 private static function put_csv($list,$title){ 16 $file_name="CSV".date("mdHis",time()).".csv"; 17 header ( ‘Content-Type: application/vnd.ms-excel‘ ); 18 header ( ‘Content-Disposition: attachment;filename=‘.$file_name ); 19 header ( ‘Cache-Control: max-age=0‘ ); 20 $file = fopen(‘php://output‘,"a"); 21 $limit=1000; 22 $calc=0; 23 foreach ($title as $v){ 24 $tit[]=iconv(‘UTF-8‘, ‘GB2312//IGNORE‘,$v); 25 } 26 fputcsv($file,$tit); 27 foreach ($list as $v){ 28 $calc++; 29 if($limit==$calc){ 30 ob_flush(); 31 flush(); 32 $calc=0; 33 } 34 foreach ($v as $t){ 35 $tarr[]=iconv(‘UTF-8‘, ‘GB2312//IGNORE‘,$t); 36 } 37 fputcsv($file,$tarr); 38 unset($tarr); 39 } 40 unset($list); 41 fclose($file); 42 exit(); 43 } 44 45 public function export_data(){ 46 $list=M("admin_menu")->field(‘id,menu,c,a‘)->select();//查询数据,可以进行处理 47 $csv_title=array(‘用户ID‘,‘菜单名‘,‘控制器名‘,‘方法名‘); 48 self::put_csv($list,$csv_title); 49 } 50 } 51 ?>
时间: 2024-11-08 21:51:58