请注意 下面说的
PHPExcel/Classes/PHPExcel
需要引入 phpexcel 插件文件 网上搜一大堆这里就不添加下载链接了哈
applocation/admin/controller/Phoexcel.php 类名:Phpexcel
1 /** 2 * 导出 excel数据 3 * @param [type] $data [数据整合] 4 * @param [type] $properties [补充信息] 5 * @param string $outputpath [文件临时保存所在的文件夹] 6 * @param [type] $borderConf [合并与分离单元格] 7 * @return [type] [description] 8 */ 9 public function exportExcelData($data,$properties,$outputpath='',$borderConf=[]) 10 { 11 //补充文档信息 12 $properties['Title']=isset($properties['Title'])?$properties['Title']:'Office 2007 xlsx Document'; 13 $properties['description']=isset($properties['description'])?$properties['description']:'The test export data of file'; 14 15 $properties['sheetTitle']=isset($properties['sheetTitle'])?$properties['sheetTitle']:'sheet1'; 16 17 $properties['keyword']=isset($properties['keyword'])?$properties['keyword']:'office 2007 openxml php'; 18 $properties['Category']=isset($properties['Category'])?$properties['Category']:'Test result file'; 19 $properties['Creator']=isset($properties['Creator'])?$properties['Creator']:'zfkj'; 20 $properties['LastModified']=isset($properties['LastModified'])?$properties['LastModified']:'zfkj'; 21 22 23 import("PHPExcel/Classes/PHPExcel", EXTEND_PATH); 24 $objPHPExcel=new \PHPExcel(); 25 // $objPHPExcel=new PExcel(); 26 27 $objPHPExcel->getProperties()->setCreator($properties['Creator']) 28 ->setLastModifiedBy($properties['LastModified']) 29 ->setTitle($properties['Title']) 30 ->setSubject($properties['Title']) 31 ->setDescription($properties['description']) 32 ->setKeywords($properties['keyword']) 33 ->setCategory($properties['Category']); 34 //设置当前的sheet 35 $objPHPExcel->setActiveSheetIndex(0); 36 //设置列数组 37 $letters_arr = array(1=>'A',2=>'B',3=>'C',4=>'D',5=>'E',6=>'F',7=>'G',8=>'H',9=>'I',10=>'J',11=>'K',12=>'L',13=>'M', 14=>'N',15=>'O',16=>'P',17=>'Q',18=>'R',19=>'S',20=>'T',21=>'U',22=>'V',23=>'W',24=>'X',25=>'Y',26=>'Z'); 38 39 //如果合并单元格 40 if(isset($borderConf['mergeCells']) && $borderConf['mergeCells']){ 41 foreach($borderConf['mergeCells'] as $mercell){ 42 // var_dump($mercell); 43 $objPHPExcel->getActiveSheet()->mergeCells($mercell['x'].":".$mercell['y']); 44 } 45 } 46 //如果分离单元格 47 if(isset($borderConf['unmerge']) && $borderConf['unmerge']){ 48 foreach($borderConf['unmerge'] as $unmercell){ 49 $objPHPExcel->getActiveSheet()->unmergeCells($unmercell['x'].":".$unmercell['y']); 50 } 51 } 52 53 54 //设置列名 55 if($data['headtitle']){ 56 $k=1;//从a1开始 57 foreach($data['headtitle'] as $tit){ 58 $tit=getutf8($tit); 59 $objPHPExcel->getActiveSheet()->setCellValue($letters_arr[$k]."1",$tit); 60 // $objPHPExcel->getActiveSheet()->getColumnDimension($letters_arr[$k])->setAutoSize(true); 61 // ->setWidth(25) 62 $k++; 63 } 64 } 65 //添加数据 66 $i=2; 67 //print_r($list);exit; 68 foreach($data['list'] as $row){ 69 //dump($row); 70 $h=1; 71 foreach ($data['listfield'] as $fid) { 72 // echo $letters_arr[$h].$i.'<br>'; 73 // echo $row[$fid].'<br>'; 74 if(isset($row[$fid])) 75 { 76 $value=getutf8($row[$fid]); 77 $objPHPExcel->getActiveSheet()->setCellValue($letters_arr[$h].$i, $value); 78 79 $ti=getutf8($data['headtitle'][$h-1]); 80 81 if((strlen($value)+7 < strlen($ti)) || (strlen($value)+2 < strlen($ti))) 82 { 83 $wid=strlen($ti); 84 $objPHPExcel->getActiveSheet()->getColumnDimension($letters_arr[$h])->setWidth($wid); 85 }else if((strlen($value)-6 == strlen($ti))){ 86 $wid=strlen($value); 87 $objPHPExcel->getActiveSheet()->getColumnDimension($letters_arr[$h])->setWidth($wid); 88 }else if(strlen($value)/5 > strlen($ti)){ 89 $objPHPExcel->getActiveSheet()->getColumnDimension($letters_arr[$h])->setWidth(40); 90 }else{ 91 $objPHPExcel->getActiveSheet()->getColumnDimension($letters_arr[$h])->setAutoSize(true); 92 } 93 // var_dump($ti,$value); 94 // var_dump(strlen($value),strlen($ti)); 95 // echo '<hr>'; 96 $objPHPExcel->getActiveSheet()->getStyle($letters_arr[$h].$i)->getAlignment()->setWrapText(true)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//自动换行 97 98 } 99 $h++; 100 } 101 $i++; 102 } 103 // die; 104 // exit; 105 //设置sheet的name 106 $objPHPExcel->getActiveSheet()->setTitle($properties['sheetTitle']); 107 108 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 109 if($outputpath!='') 110 { 111 $savefilepath=$outputpath.'/'.$properties['Title'].'.xls'; 112 $objWriter->save($savefilepath); 113 }else{ 114 header('Content-Type: application/vnd.ms-excel'); 115 header('Content-Disposition: attachment;filename='.$properties['Title'].'.xls'); 116 header('Cache-Control: max-age=0'); 117 $objWriter->save('php://output'); 118 } 119 }