phpexcel导出

jerry thinkphp 2015年11月19日 收藏
这里只粘贴了使用的代码,里面需要引用phpexcel,在上面搜索phpexcel就有很多下载的
  1. /**
  2.      * 客户导出
  3.      *
  4.      **/
  5.     public function excelExport($customerList=false){
  6.         C('OUTPUT_ENCODE', false);
  7.         import("ORG.PHPExcel.PHPExcel");
  8.         $objPHPExcel = new PHPExcel();    
  9.         $objProps = $objPHPExcel->getProperties();
  10.         $objPHPExcel->setActiveSheetIndex(0);     
  11.         $objActSheet = $objPHPExcel->getActiveSheet(); 
  12.            
  13.         $objActSheet->setTitle('Sheet1');
  14.         $ascii = 65;
  15.         $cv = '';
  16.         //联系人字段
  17.         $contacts_fields_list = array();
  18.         $contacts_fields_list[0]['field'] = 'name';
  19.         $contacts_fields_list[0]['name'] = '用户名';
  20.         $contacts_fields_list[1]['field'] = 'telephone';
  21.         $contacts_fields_list[1]['name'] = '电话';
  22.         
  23.         foreach($contacts_fields_list as $field){
  24.             $objActSheet->setCellValue($cv.chr($ascii).'1', $field['name']);
  25.             $ascii++;
  26.             if($ascii == 91){
  27.                 $ascii = 65;
  28.                 $cv .= chr(strlen($cv)+65);
  29.             }
  30.         }
  31.         
  32.         /*$where['owner_role_id'] = array('in',implode(',', getSubRoleId()));
  33.         $where['is_deleted'] = 0;*/
  34.         $list = M('user')->select();
  35.         $ascii = 65;
  36.         $cv = '';
  37.         $i = 2;
  38.         foreach($list as $val){
  39.             foreach($contacts_fields_list as $valu){
  40.                 //防止使用科学计数法,在数据前加空格
  41.                 if($valu['field'] == 'telephone' || $valu['field'] =='qq'){
  42.                     $objActSheet->setCellValue($cv.chr($ascii).$i, ' '.$val[$valu['field']]);
  43.                 }else{
  44.                     $objActSheet->setCellValue($cv.chr($ascii).$i, $val[$valu['field']]);
  45.                 }
  46.                 
  47.                 $ascii++;
  48.                 if($ascii == 91){
  49.                     $ascii = 65;
  50.                     $cv .= chr(strlen($cv)+65);
  51.                 }
  52.             }
  53.             $ascii = 65;
  54.             $cv = '';
  55.             $i++;
  56.         } 

  57.         $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  58.         ob_end_clean();
  59.         header("Content-Type: application/vnd.ms-excel;");
  60.         header("Content-Disposition:attachment;filename=5kcrm_user_".date('Y-m-d',mktime()).".xls");
  61.         header("Pragma:no-cache");
  62.         header("Expires:0");
  63.         $objWriter->save('php://output'); 
  64.     }
这里只粘贴了使用的代码,里面需要引用phpexcel,在上面搜索phpexcel就有很多下载的

再来一个简单的导出的方法:
  1. public function oexcel()
  2.     {
  3.         header('Content-Type: application/vnd.ms-excel');
  4.         header('Content-Disposition: attachment; filename=user.xls');
  5.         header('Pragma: no-cache');
  6.         header('Expires: 0');
  7.         $title = array('手机', '昵称');
  8.         $data = M('user')->field("login,uname")->select();
  9.         echo iconv('utf-8', 'gbk', implode("\t", $title)), "\n";
  10.         foreach ($data as $value) {
  11.             echo iconv('utf-8', 'gbk', ' '.implode("\t", $value)), "\n";
  12.         }

  13.     }