phpexcel 控制导出数据内容和行数
// 这里的 $company_list 是一个 $list = array(array('id'=>1,'username'=>'YQJ','sex'=>'男','age'=>24));类型的数组
public function test2Action($company_list) { // $indexKey = array('id','username','sex','age'); // $list = array(array('id'=>1,'username'=>'YQJ','sex'=>'男','age'=>24)); $indexKey = array( 'key','company_name','SHXYDM','FRDB','ESDATE','ENTSTATUS','REGCAP','REGCAPCUR','DOM','ENTTYPE','OPSCOPE'); $list = array(array('id'=>1,'username'=>'YQJ','sex'=>'男','age'=>24)); // 获取所有问题栏目 $this->exportExcel($company_list,time(),$indexKey); } function exportExcel($list,$filename,$indexKey,$startRow=2,$excel2007=false){ if(empty($filename)) $filename = time(); if( !is_array($indexKey)) return false; $header_arr = 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'); //初始化PHPExcel() $objPHPExcel = new PHPExcel(); //设置保存版本格式 if($excel2007){ $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); $filename = $filename.'.xlsx'; }else{ $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); $filename = $filename.'.xls'; } //接下来就是写数据到表格里面去 $objActSheet = $objPHPExcel->getActiveSheet(); //$startRow = 1; //设置标题 $objActSheet->setCellValue('A1', "排名"); $objActSheet->setCellValue('B1', "企业名称"); $objActSheet->setCellValue('C1', "统一社会信用代码"); $objActSheet->setCellValue('D1', "法定代表人"); $objActSheet->setCellValue('E1', "成立日期"); $objActSheet->setCellValue('F1', "经营状态"); $objActSheet->setCellValue('G1', "注册资本"); $objActSheet->setCellValue('H1', "注册资本币种"); $objActSheet->setCellValue('I1', "地址"); $objActSheet->setCellValue('J1', "企业类型"); $objActSheet->setCellValue('K1', "经营业务范围"); foreach ($list as $row) { /* p($row);die; Array ( [id] => 2146 [company_num] => N2112 [company_name] => 海南航空股份有限公司 [company_keyworlds] => 海航 [cid] => 5 [10] => 2 [11] => 2 [12] => 7 [13] => 1 [14] => 5 [15] => 2 [21] => 1 )*/ foreach ($indexKey as $key => $value){ /* p($value);die; 第一次 :id 第二次 :company_num 第三次*/ //这里是设置单元格的内容 $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]); } $startRow++; } // 下载这个表格,在浏览器输出 header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-execl"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download");; header('Content-Disposition:attachment;filename='.$filename.''); header("Content-Transfer-Encoding:binary"); $objWriter->save('php://output'); }