我使用的是tp3.2框架(下载地址:http://www.thinkphp.cn/topic/38123.html)

1.首先要下载PhpExcel类库,放在如下图目录下

2.调用方法

 1 public function exportExcel($expTitle,$expCellName,$expTableData)
 2 {
 3     $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
 4     $fileName = $_SESSION['account'].date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
 5     $cellNum = count($expCellName);
 6     $dataNum = count($expTableData);
 7     vendor("PHPExcel.PHPExcel");//引入类库,一定要引对,否则会报错找不到这个类
 8     $objPHPExcel = new \PHPExcel();
 9     $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
10 
11     $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
12     // $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
13     for($i=0;$i<$cellNum;$i++){
14         $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
15     }
16     // Miscellaneous glyphs, UTF-8
17     for($i=0;$i<$dataNum;$i++){
18         for($j=0;$j<$cellNum;$j++){
19             $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
20         }
21     }
22 
23     header('pragma:public');
24     header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
25     header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
26     ob_clean();//把数据从PHP的缓冲(buffer)中释放出来。
27     flush();//把不在缓冲(buffer)中的或者说是被释放出来的数据发送到浏览器。
28     $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');
29     $objWriter->save('php://output');
30     exit;
31 }
32 /**
33  *
34  * 导出Excel
35  */
36 function daochu()
37 {//导出Excel
38     $xlsName  = "User";
39     $xlsCell  = array(
40         array('id','账号序列'),
41         array('u_name','用户名字'),
42         array('u_addres','地址'),
43         array('u_tel','电话'),
44         array('u_card','身份证号'),
45         array('u_time','申报时间'),
46         // array('u_imgz','申报时间'),
47         // array('u_imgf','申报时间'),
48         array('b_one_beizhu','办公人员备注'),
49         array('b_shenhe_p','办公室审核人员'),
50         array('water_kj','水表口径'),
51         array('water_wz','水表具体位置'),
52         array('water_quyu','区域位置'),
53         array('water_start_time','施工日期'),
54         array('water_start_peo','施工人员'),
55         array('water_start_jingli','施工经理'),
56         array('water_beizhu','施工经理备注'),
57         array('water_xingzhi','用水性质'),
58         array('watch_num','水表底数'),
59         array('watch_time','抄表日期'),
60         array('watch_peo','抄表人员'),
61         array('moneyjingli_name','收费经理'),
62         array('moneyjingli_beizhu','收费经理备注'),
63 
64         array('jingli_name','经理名称')
65 
66     );
67     $xlsModel = M('api_aliucheng');
68 
69     $xlsData  = $xlsModel->Field('id,u_name,u_addres,u_tel,u_card,u_time,b_one_beizhu,b_shenhe_p,water_kj,water_wz,water_quyu,water_start_time,water_start_peo,water_start_jingli,water_beizhu,water_xingzhi,watch_num,watch_time,watch_peo,moneyjingli_name,moneyjingli_beizhu,jingli_name')->where(array('jingli_state_san'=>1))->select();
70 
71     $this->exportExcel($xlsName,$xlsCell,$xlsData);
72 
73 }

 

3.导出结果(上面标红的地方要特别注意)



posted on 2018-05-22 17:35  巴黎夜雨……  阅读(349)  评论(0编辑  收藏  举报