ThinkPHP5 + PHPExcel 批量匯入匯出(部分注意事項)
阿新 • • 發佈:2018-12-17
在網上下載PHPExcel資源包,置於vender資料夾之下,也可以點選我的網盤分享:https://pan.baidu.com/s/1v2_oMMSvwTcebG9F3YQJqQ
先說兩個注意事項:
1、部分php7環境的朋友可能在匯出的時候不能執行:$objWriter->save('php://output') 語句(我就是這種情況),此時將PHPExcel外掛中(PHPExcel/PHPExcel/Calculation/Functions.php中576行的break註釋掉就是)
2、關於日期的處理,PHPExcel中處理日期的方式如下:
$op_Time = gmdate("Y-m-d", \PHPExcel_Shared_Date::ExcelToPHP($objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue()));
$data['op_Time'] = strtotime($op_Time);
/* * 代理商資訊匯出 */ public function downAgent() { //這幾個欄位是篩選匯出 if(input('us_truename')){ //姓名 $map['us_truename'] = array('like','%'.input('us_truename').'%'); } if(input('ag_idcard')){ //身份證號碼 $map['ag_idcard'] = input('ag_idcard'); } if(input('ag_cityid3') != ''){ //代理地區 $map['ag_cityid'] = input('ag_cityid3'); }elseif(input('ag_cityid2') != ''){ $map['ag_cityid'] = input('ag_cityid2'); }elseif(input('ag_cityid1') != ''){ $map['ag_cityid'] = input('ag_cityid1'); } $map['us_role'] = 2; //代理商 $map['us_status'] = array('gt','-1'); $info = Db::table('tb_users')->field('us_truename,ag_phone_number,ag_idcard,ag_address,ci_name,ag_company_addess,ag_ent_name,ag_offline_num,ag_tax_payment,ag_billing_address,ag_billing_phone,ag_billing_bank,ag_billing_number,ag_info')->join('tb_agent','tb_agent.ag_usid=tb_users.us_id')->join('tb_city','tb_city.ci_id=tb_agent.ag_cityid')->where($map)->select(); //匯出資料 $file_name = date('Y-m-d_His').'.xls'; ob_clean(); vendor("PHPExcel.PHPExcel"); //引入檔案 $objPHPExcel = new \PHPExcel(); $objPHPExcel->getActiveSheet()->SetCellValue('A1', '姓名'); $objPHPExcel->getActiveSheet()->SetCellValue('B1', '電話'); $objPHPExcel->getActiveSheet()->SetCellValue('C1', '身份證號碼'); $objPHPExcel->getActiveSheet()->SetCellValue('D1', '身份證地址'); $objPHPExcel->getActiveSheet()->SetCellValue('E1', '代理地區'); $objPHPExcel->getActiveSheet()->SetCellValue('F1', '辦公地點'); $objPHPExcel->getActiveSheet()->SetCellValue('G1', '線下人員人數'); $objPHPExcel->getActiveSheet()->SetCellValue('H1', '企業名稱'); $objPHPExcel->getActiveSheet()->SetCellValue('I1', '納稅人識別號'); $objPHPExcel->getActiveSheet()->SetCellValue('J1', '開票地址'); $objPHPExcel->getActiveSheet()->SetCellValue('K1', '開票電話'); $objPHPExcel->getActiveSheet()->SetCellValue('L1', '開票銀行'); $objPHPExcel->getActiveSheet()->SetCellValue('M1', '開票賬號'); $objPHPExcel->getActiveSheet()->SetCellValue('N1', '徵信(備註)'); $i = 2; foreach($info as $key => $value){ $objPHPExcel->getActiveSheet()->SetCellValue('A'.$i,''.$value['us_truename']); $objPHPExcel->getActiveSheet()->SetCellValue('B'.$i,' '.$value['ag_phone_number']); $objPHPExcel->getActiveSheet()->SetCellValue('C'.$i,' '.$value['ag_idcard']); $objPHPExcel->getActiveSheet()->SetCellValue('D'.$i,''.$value['ag_address']); $objPHPExcel->getActiveSheet()->SetCellValue('E'.$i,''.$value['ci_name']); $objPHPExcel->getActiveSheet()->SetCellValue('F'.$i,''.$value['ag_company_addess']); $objPHPExcel->getActiveSheet()->SetCellValue('G'.$i,''.$value['ag_ent_name']); $objPHPExcel->getActiveSheet()->SetCellValue('H'.$i,''.$value['ag_offline_num']); $objPHPExcel->getActiveSheet()->SetCellValue('I'.$i,''.$value['ag_tax_payment']); $objPHPExcel->getActiveSheet()->SetCellValue('J'.$i,''.$value['ag_billing_address']); $objPHPExcel->getActiveSheet()->SetCellValue('K'.$i,' '.$value['ag_billing_phone']); $objPHPExcel->getActiveSheet()->SetCellValue('L'.$i,''.$value['ag_billing_bank']); $objPHPExcel->getActiveSheet()->SetCellValue('M'.$i,' '.$value['ag_billing_number']); $objPHPExcel->getActiveSheet()->SetCellValue('N'.$i,''.$value['ag_info']); $i++; } $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename='.$file_name ); header('Cache-Control: max-age=0'); //輸出指定目錄 $objWriter->save('php://output'); die; } /* * 批量匯入代理商詳情 */ public function upAgent(){ if(request()->ispost()){ // 獲取表單上傳檔案 $file = request()->file('agent_file'); if(empty($file)){ $this->error('請選擇上傳檔案!'); }else{ //檔案位置 $moveUrl = ROOT_PATH.'public'.DS.'upload/files/agent/'; //移動檔案 $info = $file->validate(['size'=>12428800,'ext'=>'xls,xlsx'])->rule('uniqid')->move($moveUrl); if($info){ // 成功上傳後 獲取上傳資訊 $file_name = $moveUrl.DS.$info->getFilename(); //匯入 vendor("PHPExcel.PHPExcel"); $extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));//判斷匯入表格字尾格式 if($extension == 'xlsx') { $objReader =\PHPExcel_IOFactory::createReader('Excel2007'); $objPHPExcel =$objReader->load($file_name, $encode = 'utf-8'); }else if($extension == 'xls'){ $objReader =\PHPExcel_IOFactory::createReader('Excel5'); $objPHPExcel =$objReader->load($file_name, $encode = 'utf-8'); } $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow();//取得總行數 $highestColumn = $sheet->getHighestColumn(); //取得總列數 $num = 0; for($i = 2; $i <= $highestRow; $i++){ $map['us_truename'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue(); $map['us_name'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue(); $map['us_number'] = onlyNumber(); $map['us_password'] = sha1(md5('123456')); $map['us_time'] = time(); $map['us_role'] = 2; //代理商 $uid = Db::name('users')->insertGetId($map); if($uid){ $data['ag_usid'] = $uid; $data['ag_phone_number'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue(); $data['ag_idcard'] = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue(); $data['ag_address'] = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue(); $where['ci_name'] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue(); $cityInfo = Db::name('city')->field('ci_id')->where($where)->find(); $data['ag_cityid'] = $cityInfo['ci_id'] ? $cityInfo['ci_id'] : 0; $dataU['us_cityId'] = $cityInfo['ci_id'] ? $cityInfo['ci_id'] : 0; @Db::name('users')->where('us_id ='.$uid)->update($dataU); $data['ag_company_addess'] = $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue(); $data['ag_offline_num'] = $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue(); $data['ag_ent_name'] = $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue(); $data['ag_tax_payment'] = $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue(); $data['ag_billing_address'] = $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue(); $data['ag_billing_phone'] = $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue(); $data['ag_billing_bank'] = $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue(); $data['ag_billing_number'] = $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue(); $data['ag_info'] = $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue(); $info = Db::name('agent')->insert($data); if($info){ $num++; }else{ break; } }else{ break; } } //刪除excel檔案 unlink($file_name); if($num > 0){ $this->success('匯入成功'.$num.'條資料',url('agent/agentList'),3); }else{ $this->error('匯入第'.$num.'條失敗'); } }else{ // 上傳失敗獲取錯誤資訊 $this->error($file->getError()); } } } }