phpexcel根據傳來的欄位,生成不同的列名,傳多則多,傳少則少
阿新 • • 發佈:2018-12-10
$select_field = I('select_field'); if($select_field){ $indexKey = explode(',',$select_field); }else{ $indexKey = array( //'id', 'employee_name', //姓名 'job_number', //工號 'shi_days', //當月實際出勤天數 'ying_days', //當月應出勤天數 'department_name', //部門 'attence_unit_name', //考勤組 'chidao_count', //遲到次數 'chidao_m', //遲到總時長 'zaotui_count', //早退次數 'zaotui_m', //早退總時長 'weida_count', //漏打卡次數 'waiqin_count', //外勤次數 'kuanggong_days', //曠工次數 'gongzuo_jiaban', //工作日加班 'xiuxi_jiaban', //休息日加班 'jiejiari_jiaban', //節假日加班 'month_qingjia_days', //本月共請假天數 'use_nianjia_days', //使用年假 'limit_jianjia_hours', //年假剩餘天數 //nianjia_limit_days 'use_tiaoxiu_days', //使用調休 'limit_tiaoxiu_hours', //調休剩餘天數 //tiaoxiu_limit_days 'qingjia_shijia_days', //事假天數 shijia_days 'qingjia_binjia_days', //病假 sick_leave 'qingjia_hunjia_days', //婚假 marriage_leave 'check_leave', //產檢假 'maternity_leave', //產假 'paternity_leave', //陪產假 'lactation_leave', //哺乳假 'care_leave', //看護假 'parental_leave', //探親假 'white_leave', //喪假 ); } $lists = M('collect_attence')->where($sqlwhere)->select(); $this->attendanceExcel($lists,'考勤報表匯出'.time(),$indexKey);
//版本二 start //根據傳來的欄位,excel展示相應的資料。沒有傳來的欄位excel列名不顯示 $cell_name = array( 'employee_name'=>'姓名', 'job_number'=>'工號', 'shi_days'=>'當月實際出勤天數', 'ying_days'=>'當月應出勤天數', 'department_name'=>'部門', 'attence_unit_name'=>'考勤組', 'chidao_count'=>'遲到次數', 'chidao_m'=>'遲到總時長', 'zaotui_count'=>'早退次數', 'zaotui_m'=>'早退總時長', 'weida_count'=>'漏打卡次數', 'waiqin_count'=>'外勤次數', 'kuanggong_days'=>'曠工次數', 'gongzuo_jiaban'=>'工作日加班', 'xiuxi_jiaban'=>'休息日加班', 'jiejiari_jiaban'=>'節假日加班', 'month_qingjia_days'=>'本月共請假天數', 'use_nianjia_days'=>'使用年假', 'limit_jianjia_hours'=>'年假剩餘小時數', 'use_tiaoxiu_days'=>'使用調休', 'limit_tiaoxiu_hours'=>'調休剩餘小時數', 'qingjia_shijia_days'=>'事假天數', 'qingjia_binjia_days'=>'病假', 'qingjia_hunjia_days'=>'婚假', 'check_leave'=>'產檢假', 'maternity_leave'=>'產假', 'paternity_leave'=>'陪產假', 'lactation_leave'=>'哺乳假', 'care_leave'=>'看護假', 'parental_leave'=>'探親假', 'white_leave'=>'喪假' ); $cl_name = array('A1','B1','C1','D1','E1','F1','G1','H1','I1','J1','K1','L1','M1','N1','O1','P1','Q1','R1','S1', 'T1','U1','V1','W1','X1','Y1','Z1','AA1','AB1','AC1','AD1','AE1'); $objActSheet = $objPHPExcel->getActiveSheet(); foreach($cl_name as $key=>$val){ $objActSheet->setCellValue($val,$cell_name[$indexKey[$key]]); }
完整程式碼
//月考勤表 匯出報表 public function attendance(){ $company_id = I('post.company_id'); $sqlwhere['company_id'] = I('post.company_id'); $department_id = I('post.department_id','','htmlspecialchars'); $employee_name = I('post.employee_name','','htmlspecialchars'); if($department_id!='')$sqlwhere['department_id'] =I('post.department_id','','htmlspecialchars'); if($employee_name!='')$sqlwhere['_string'] ='employee_name like "%'.I('post.employee_name','','htmlspecialchars').'%" '; //所要匯出的欄位 $select_field = I('select_field'); if($select_field){ $indexKey = explode(',',$select_field); }else{ $indexKey = array( //'id', 'employee_name', //姓名 'job_number', //工號 'shi_days', //當月實際出勤天數 'ying_days', //當月應出勤天數 'department_name', //部門 'attence_unit_name', //考勤組 'chidao_count', //遲到次數 'chidao_m', //遲到總時長 'zaotui_count', //早退次數 'zaotui_m', //早退總時長 'weida_count', //漏打卡次數 'waiqin_count', //外勤次數 'kuanggong_days', //曠工次數 'gongzuo_jiaban', //工作日加班 'xiuxi_jiaban', //休息日加班 'jiejiari_jiaban', //節假日加班 'month_qingjia_days', //本月共請假天數 'use_nianjia_days', //使用年假 'limit_jianjia_hours', //年假剩餘天數 //nianjia_limit_days 'use_tiaoxiu_days', //使用調休 'limit_tiaoxiu_hours', //調休剩餘天數 //tiaoxiu_limit_days 'qingjia_shijia_days', //事假天數 shijia_days 'qingjia_binjia_days', //病假 sick_leave 'qingjia_hunjia_days', //婚假 marriage_leave 'check_leave', //產檢假 'maternity_leave', //產假 'paternity_leave', //陪產假 'lactation_leave', //哺乳假 'care_leave', //看護假 'parental_leave', //探親假 'white_leave', //喪假 ); } $lists = M('collect_attence')->where($sqlwhere)->select(); $this->attendanceExcel($lists,'考勤報表匯出'.time(),$indexKey); } public function attendanceExcel($list,$filename,$indexKey,$startRow=1,$excel2007=false){ vendor('PHPExcel.PHPExcel'); 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','AA','AB','AC','AD','AE','AF'); //初始化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(); $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', "漏打卡次數"); $objActSheet->setCellValue('L1', "外勤次數"); $objActSheet->setCellValue('M1', "曠工次數"); $objActSheet->setCellValue('N1', "工作日加班"); $objActSheet->setCellValue('O1', "休息日加班"); $objActSheet->setCellValue('P1', "節假日加班"); $objActSheet->setCellValue('Q1', "本月共請假天數"); $objActSheet->setCellValue('R1', "使用年假"); $objActSheet->setCellValue('S1', "年假剩餘小時數");//天數 $objActSheet->setCellValue('T1', "使用調休"); $objActSheet->setCellValue('U1', "調休剩餘小時數");//天數 $objActSheet->setCellValue('V1', "事假天數"); $objActSheet->setCellValue('W1', "病假"); $objActSheet->setCellValue('X1', "婚假"); $objActSheet->setCellValue('Y1', "產檢假"); $objActSheet->setCellValue('Z1', "產假"); $objActSheet->setCellValue('AA1', "陪產假"); $objActSheet->setCellValue('AB1', "哺乳假"); $objActSheet->setCellValue('AC1', "看護假"); $objActSheet->setCellValue('AD1', "探親假"); $objActSheet->setCellValue('AE1', "喪假");*/ //版本二 start //根據傳來的欄位,excel展示相應的資料。沒有傳來的欄位excel列名不顯示 $cell_name = array( 'employee_name'=>'姓名', 'job_number'=>'工號', 'shi_days'=>'當月實際出勤天數', 'ying_days'=>'當月應出勤天數', 'department_name'=>'部門', 'attence_unit_name'=>'考勤組', 'chidao_count'=>'遲到次數', 'chidao_m'=>'遲到總時長', 'zaotui_count'=>'早退次數', 'zaotui_m'=>'早退總時長', 'weida_count'=>'漏打卡次數', 'waiqin_count'=>'外勤次數', 'kuanggong_days'=>'曠工次數', 'gongzuo_jiaban'=>'工作日加班', 'xiuxi_jiaban'=>'休息日加班', 'jiejiari_jiaban'=>'節假日加班', 'month_qingjia_days'=>'本月共請假天數', 'use_nianjia_days'=>'使用年假', 'limit_jianjia_hours'=>'年假剩餘小時數', 'use_tiaoxiu_days'=>'使用調休', 'limit_tiaoxiu_hours'=>'調休剩餘小時數', 'qingjia_shijia_days'=>'事假天數', 'qingjia_binjia_days'=>'病假', 'qingjia_hunjia_days'=>'婚假', 'check_leave'=>'產檢假', 'maternity_leave'=>'產假', 'paternity_leave'=>'陪產假', 'lactation_leave'=>'哺乳假', 'care_leave'=>'看護假', 'parental_leave'=>'探親假', 'white_leave'=>'喪假' ); $cl_name = array('A1','B1','C1','D1','E1','F1','G1','H1','I1','J1','K1','L1','M1','N1','O1','P1','Q1','R1','S1', 'T1','U1','V1','W1','X1','Y1','Z1','AA1','AB1','AC1','AD1','AE1'); $objActSheet = $objPHPExcel->getActiveSheet(); foreach($cl_name as $key=>$val){ $objActSheet->setCellValue($val,$cell_name[$indexKey[$key]]); } //版本二 end $startRow = 2; foreach ($list as $row) { foreach ($indexKey as $key => $value) { //這裡是設定單元格的內容 $objActSheet->setCellValue($header_arr[$key] . $startRow, $row[$value]); } $startRow++; } header('pragma:public'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$filename.'.xls"'); header("Content-Disposition:attachment;filename=$filename.xls");//attachment新視窗列印inline本視窗列印 header('Cache-Control: max-age=0'); //$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel); $excle_path = C('APP_PATH').(substr($this->saveExcelToLocalFile($objWriter, $filename),2)); $response = array( 'success' => true, 'url' => $excle_path, ); if ($response) { $this->ajaxReturn($response, "json"); }