码迷,mamicode.com
首页 > Web开发 > 详细

thinkphp导出csv格式的表格

时间:2016-04-21 13:23:05      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

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

 

thinkphp导出csv格式的表格

标签:

原文地址:http://www.cnblogs.com/719907411hl/p/5416298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!