function customer_daochu() { /** * 客户名单导出 * */ $customer = D('WcoaUCustomers'); $data = $customer->join("customers_system p on customers.customersid = p.customers_system_id ")->field("customersid,customername,customercode,phone,belong_deptid,belong_employeesid")->select(); $list = array(); $list1 = array(); foreach($data as $v) { $list1[0] = $v['customersid']; $list1[1] = $v['customername']; $list1[2] = $v['customercode']; $list1[3] = $v['phone']; $dept = D('Dept')->field('deptname')->where("deptid = {$v['belong_deptid']}")->find(); $list1[4] = $dept['deptname']; $em = D('Employees')->field('employeename')->where("employeesid = {$v['belong_employeesid']}")->find(); $list1[5] = $em['employeename']; array_push($list,$list1); } $headArr = array('id','姓名','编号','手机','所属部门','所属员工'); $fileName = "customer"; $this->exportExcel($fileName,$headArr,$list); } public function exportExcel($expTitle,$expCellName,$expTableData) //$expTitle=>表的主题,$expCellName=>表头名(列名),$expTableData=>数据(以下采用数字索引数组) { $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称 $fileName = '客户表'.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定 $cellNum = count($expCellName); //多少列 $dataNum = count($expTableData); //多少行 Vendor("PHPExcel"); $objPHPExcel = new PHPExcel(); //初始化 $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'); $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格 $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $fileName); //标题 for($i=0;$i<$cellNum;$i++){ $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i]); //表头(列名) } // Miscellaneous glyphs, UTF-8 for($i=0;$i<$dataNum;$i++){ for($j=0;$j<$cellNum;$j++){ $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$j]); //内容 } } //可以改格式,剩下不懂了 header('pragma:public'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xlsx"'); header("Content-Disposition:attachment;filename=$fileName.xlsx");//attachment新窗口打印inline本窗口打印 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); exit; }
PHPExcel的存放位置跟导入excel位置一样!